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
;
289 extern u_long route_generation
;
290 extern int apple_hwcksum_rx
;
293 * IP initialization: fill in IP protocol switch table.
294 * All protocols not implemented in kernel go to raw IP protocol handler.
299 register struct protosw
*pr
;
301 static ip_initialized
= 0;
305 TAILQ_INIT(&in_ifaddrhead
);
306 pr
= pffindproto(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
309 for (i
= 0; i
< IPPROTO_MAX
; i
++)
311 for (pr
= inetdomain
.dom_protosw
; pr
; pr
= pr
->pr_next
)
312 { if(!((unsigned int)pr
->pr_domain
)) continue; /* If uninitialized, skip */
313 if (pr
->pr_domain
->dom_family
== PF_INET
&&
314 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
)
315 ip_protox
[pr
->pr_protocol
] = pr
;
317 for (i
= 0; i
< IPREASS_NHASH
; i
++)
318 ipq
[i
].next
= ipq
[i
].prev
= &ipq
[i
];
320 maxnipq
= nmbclusters
/ 4;
321 ip_maxfragpackets
= nmbclusters
/ 4;
324 ip_id
= time_second
& 0xffff;
326 ipintrq
.ifq_maxlen
= ipqmaxlen
;
331 /* Initialize the PF_INET domain, and add in the pre-defined protos */
335 register struct protosw
*pr
;
336 register struct domain
*dp
;
337 static inetdomain_initted
= 0;
338 extern int in_proto_count
;
340 if (!inetdomain_initted
)
342 kprintf("Initing %d protosw entries\n", in_proto_count
);
345 for (i
=0, pr
= &inetsw
[0]; i
<in_proto_count
; i
++, pr
++)
346 net_add_proto(pr
, dp
);
347 inetdomain_initted
= 1;
351 static struct sockaddr_in ipaddr
= { sizeof(ipaddr
), AF_INET
};
352 static struct route ipforward_rt
;
355 * Ip input routine. Checksum and byte swap header. If fragmented
356 * try to reassemble. Process options. Pass to next level.
359 ip_input(struct mbuf
*m
)
363 struct in_ifaddr
*ia
= NULL
;
364 int i
, hlen
, mff
, checkif
;
366 u_int16_t divert_cookie
; /* firewall cookie */
367 struct in_addr pkt_dst
;
369 u_int16_t divert_info
= 0; /* packet divert/tee info */
371 struct ip_fw_chain
*rule
= NULL
;
374 /* Get and reset firewall cookie */
375 divert_cookie
= ip_divert_cookie
;
376 ip_divert_cookie
= 0;
381 #if IPFIREWALL && DUMMYNET
383 * dummynet packet are prepended a vestigial mbuf with
384 * m_type = MT_DUMMYNET and m_data pointing to the matching
387 if (m
->m_type
== MT_DUMMYNET
) {
388 rule
= (struct ip_fw_chain
*)(m
->m_data
) ;
390 ip
= mtod(m
, struct ip
*);
391 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
398 if (m
== NULL
|| (m
->m_flags
& M_PKTHDR
) == 0)
399 panic("ip_input no HDR");
403 if (m
->m_pkthdr
.len
< sizeof(struct ip
))
406 if (m
->m_len
< sizeof (struct ip
) &&
407 (m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
408 ipstat
.ips_toosmall
++;
411 ip
= mtod(m
, struct ip
*);
413 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
414 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
416 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
417 ipstat
.ips_badvers
++;
421 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
422 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
423 ipstat
.ips_badhlen
++;
426 if (hlen
> m
->m_len
) {
427 if ((m
= m_pullup(m
, hlen
)) == 0) {
428 ipstat
.ips_badhlen
++;
431 ip
= mtod(m
, struct ip
*);
434 /* 127/8 must not appear on wire - RFC1122 */
435 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
436 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
437 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
439 ipstat
.ips_badaddr
++;
445 /* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */
446 if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
447 IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) {
448 ip_linklocal_stat
.iplls_in_total
++;
449 if (ip
->ip_ttl
!= MAXTTL
) {
450 ip_linklocal_stat
.iplls_in_badttl
++;
451 /* Silently drop link local traffic with bad TTL */
452 if (ip_linklocal_in_allowbadttl
!= 0)
456 if ((m
->m_pkthdr
.rcvif
->if_hwassist
== 0) || (apple_hwcksum_rx
== 0) ||
457 ((m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
) && ip
->ip_p
!= IPPROTO_TCP
))
458 m
->m_pkthdr
.csum_flags
= 0; /* invalidate HW generated checksum flags */
460 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
461 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
463 sum
= in_cksum(m
, hlen
);
471 * Convert fields to host representation.
474 if (ip
->ip_len
< hlen
) {
481 * Check that the amount of data in the buffers
482 * is as at least much as the IP header would have us expect.
483 * Trim mbufs if longer than we expect.
484 * Drop packet if shorter than we expect.
486 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
488 ipstat
.ips_tooshort
++;
491 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
492 /* Invalidate hwcksuming */
493 m
->m_pkthdr
.csum_flags
= 0;
494 m
->m_pkthdr
.csum_data
= 0;
496 if (m
->m_len
== m
->m_pkthdr
.len
) {
497 m
->m_len
= ip
->ip_len
;
498 m
->m_pkthdr
.len
= ip
->ip_len
;
500 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
504 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
))
510 * Right now when no processing on packet has done
511 * and it is still fresh out of network we do our black
513 * - Firewall: deny/allow/divert
514 * - Xlate: translate packet's addr/port (NAT).
515 * - Pipe: pass pkt through dummynet.
516 * - Wrap: fake packet's addr/port <unimpl.>
517 * - Encapsulate: put it in another IP and send out. <unimp.>
520 #if defined(IPFIREWALL) && defined(DUMMYNET)
524 * Check if we want to allow this packet to be processed.
525 * Consider it to be bad if not.
530 if ((*fr_checkp
)(ip
, hlen
, m
->m_pkthdr
.rcvif
, 0, &m1
) || !m1
)
532 ip
= mtod(m
= m1
, struct ip
*);
534 if (fw_enable
&& ip_fw_chk_ptr
) {
535 #if IPFIREWALL_FORWARD
537 * If we've been forwarded from the output side, then
538 * skip the firewall a second time
542 #endif /* IPFIREWALL_FORWARD */
544 * See the comment in ip_output for the return values
545 * produced by the firewall.
547 i
= (*ip_fw_chk_ptr
)(&ip
,
548 hlen
, NULL
, &divert_cookie
, &m
, &rule
, &ip_fw_fwd_addr
);
549 if ( (i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
554 ip
= mtod(m
, struct ip
*); /* just in case m changed */
555 if (i
== 0 && ip_fw_fwd_addr
== NULL
) /* common case */
558 if ((i
& IP_FW_PORT_DYNT_FLAG
) != 0) {
559 /* send packet to the appropriate pipe */
560 dummynet_io(i
&0xffff,DN_TO_IP_IN
,m
,NULL
,NULL
,0, rule
);
565 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
566 /* Divert or tee packet */
571 #if IPFIREWALL_FORWARD
572 if (i
== 0 && ip_fw_fwd_addr
!= NULL
)
576 * if we get here, the packet must be dropped
584 * Process options and, if not destined for us,
585 * ship it on. ip_dooptions returns 1 when an
586 * error was detected (causing an icmp message
587 * to be sent and the original packet to be freed).
589 ip_nhops
= 0; /* for source routed packets */
590 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
)) {
591 #if IPFIREWALL_FORWARD
592 ip_fw_fwd_addr
= NULL
;
597 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
598 * matter if it is destined to another node, or whether it is
599 * a multicast one, RSVP wants it! and prevents it from being forwarded
600 * anywhere else. Also checks if the rsvp daemon is running before
601 * grabbing the packet.
603 if (rsvp_on
&& ip
->ip_p
==IPPROTO_RSVP
)
607 * Check our list of addresses, to see if the packet is for us.
608 * If we don't have any addresses, assume any unicast packet
609 * we receive might be for us (and let the upper layers deal
612 if (TAILQ_EMPTY(&in_ifaddrhead
) &&
613 (m
->m_flags
& (M_MCAST
|M_BCAST
)) == 0)
617 * Cache the destination address of the packet; this may be
618 * changed by use of 'ipfw fwd'.
620 pkt_dst
= ip_fw_fwd_addr
== NULL
?
621 ip
->ip_dst
: ip_fw_fwd_addr
->sin_addr
;
624 * Enable a consistency check between the destination address
625 * and the arrival interface for a unicast packet (the RFC 1122
626 * strong ES model) if IP forwarding is disabled and the packet
627 * is not locally generated and the packet is not subject to
630 * XXX - Checking also should be disabled if the destination
631 * address is ipnat'ed to a different interface.
633 * XXX - Checking is incompatible with IP aliases added
634 * to the loopback interface instead of the interface where
635 * the packets are received.
637 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
638 ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) &&
639 (ip_fw_fwd_addr
== NULL
);
641 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
) {
642 #define satosin(sa) ((struct sockaddr_in *)(sa))
644 if (IA_SIN(ia
)->sin_addr
.s_addr
== INADDR_ANY
)
648 * If the address matches, verify that the packet
649 * arrived via the correct interface if checking is
652 if (IA_SIN(ia
)->sin_addr
.s_addr
== pkt_dst
.s_addr
&&
653 (!checkif
|| ia
->ia_ifp
== m
->m_pkthdr
.rcvif
))
656 * Only accept broadcast packets that arrive via the
657 * matching interface. Reception of forwarded directed
658 * broadcasts would be handled via ip_forward() and
659 * ether_output() with the loopback into the stack for
660 * SIMPLEX interfaces handled by ether_output().
662 if ((!checkif
|| ia
->ia_ifp
== m
->m_pkthdr
.rcvif
) &&
663 ia
->ia_ifp
&& ia
->ia_ifp
->if_flags
& IFF_BROADCAST
) {
664 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
667 if (ia
->ia_netbroadcast
.s_addr
== pkt_dst
.s_addr
)
671 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
672 struct in_multi
*inm
;
675 * If we are acting as a multicast router, all
676 * incoming multicast packets are passed to the
677 * kernel-level multicast forwarding function.
678 * The packet is returned (relatively) intact; if
679 * ip_mforward() returns a non-zero value, the packet
680 * must be discarded, else it may be accepted below.
682 if (ip_mforward(ip
, m
->m_pkthdr
.rcvif
, m
, 0) != 0) {
683 ipstat
.ips_cantforward
++;
689 * The process-level routing daemon needs to receive
690 * all multicast IGMP packets, whether or not this
691 * host belongs to their destination groups.
693 if (ip
->ip_p
== IPPROTO_IGMP
)
695 ipstat
.ips_forward
++;
698 * See if we belong to the destination multicast group on the
701 IN_LOOKUP_MULTI(ip
->ip_dst
, m
->m_pkthdr
.rcvif
, inm
);
703 ipstat
.ips_notmember
++;
709 if (ip
->ip_dst
.s_addr
== (u_long
)INADDR_BROADCAST
)
711 if (ip
->ip_dst
.s_addr
== INADDR_ANY
)
714 /* Allow DHCP/BootP responses through */
715 if (m
->m_pkthdr
.rcvif
!= NULL
716 && (m
->m_pkthdr
.rcvif
->if_eflags
& IFEF_AUTOCONFIGURING
)
717 && hlen
== sizeof(struct ip
)
718 && ip
->ip_p
== IPPROTO_UDP
) {
720 if (m
->m_len
< sizeof(struct udpiphdr
)
721 && (m
= m_pullup(m
, sizeof(struct udpiphdr
))) == 0) {
722 udpstat
.udps_hdrops
++;
725 ui
= mtod(m
, struct udpiphdr
*);
726 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
729 ip
= mtod(m
, struct ip
*); /* in case it changed */
732 #if defined(NFAITH) && 0 < NFAITH
734 * FAITH(Firewall Aided Internet Translator)
736 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
== IFT_FAITH
) {
738 if (ip
->ip_p
== IPPROTO_TCP
|| ip
->ip_p
== IPPROTO_ICMP
)
746 * Not for us; forward if possible and desirable.
748 if (ipforwarding
== 0) {
749 ipstat
.ips_cantforward
++;
753 #if IPFIREWALL_FORWARD
754 ip_fw_fwd_addr
= NULL
;
760 /* Darwin does not have an if_data in ifaddr */
761 /* Count the packet in the ip address stats */
763 ia
->ia_ifa
.if_ipackets
++;
764 ia
->ia_ifa
.if_ibytes
+= m
->m_pkthdr
.len
;
769 * If offset or IP_MF are set, must reassemble.
770 * Otherwise, nothing need be done.
771 * (We could look in the reassembly queue to see
772 * if the packet was previously fragmented,
773 * but it's not worth the time; just let them time out.)
775 if (ip
->ip_off
& (IP_MF
| IP_OFFMASK
| IP_RF
)) {
778 * Reassembly should be able to treat a mbuf cluster, for later
779 * operation of contiguous protocol headers on the cluster. (KAME)
781 if (m
->m_flags
& M_EXT
) { /* XXX */
782 if ((m
= m_pullup(m
, hlen
)) == 0) {
783 ipstat
.ips_toosmall
++;
784 #if IPFIREWALL_FORWARD
785 ip_fw_fwd_addr
= NULL
;
789 ip
= mtod(m
, struct ip
*);
792 sum
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
794 * Look for queue of fragments
797 for (fp
= ipq
[sum
].next
; fp
!= &ipq
[sum
]; fp
= fp
->next
)
798 if (ip
->ip_id
== fp
->ipq_id
&&
799 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
800 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
801 ip
->ip_p
== fp
->ipq_p
)
806 /* check if there's a place for the new queue */
807 if (nipq
> maxnipq
) {
809 * drop something from the tail of the current queue
810 * before proceeding further
812 if (ipq
[sum
].prev
== &ipq
[sum
]) { /* gak */
813 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
814 if (ipq
[i
].prev
!= &ipq
[i
]) {
815 ip_freef(ipq
[i
].prev
);
820 ip_freef(ipq
[sum
].prev
);
824 * Adjust ip_len to not reflect header,
825 * set ip_mff if more fragments are expected,
826 * convert offset of this to bytes.
829 mff
= (ip
->ip_off
& IP_MF
) != 0;
832 * Make sure that fragments have a data length
833 * that's a non-zero multiple of 8 bytes.
835 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
836 ipstat
.ips_toosmall
++; /* XXX */
839 m
->m_flags
|= M_FRAG
;
841 /* Clear the flag in case packet comes from loopback */
842 m
->m_flags
&= ~M_FRAG
;
847 * If datagram marked as having more fragments
848 * or if this is not the first fragment,
849 * attempt reassembly; if it succeeds, proceed.
851 if (mff
|| ip
->ip_off
) {
852 ipstat
.ips_fragments
++;
853 m
->m_pkthdr
.header
= ip
;
856 fp
, &ipq
[sum
], &divert_info
, &divert_cookie
);
858 m
= ip_reass(m
, fp
, &ipq
[sum
]);
861 #if IPFIREWALL_FORWARD
862 ip_fw_fwd_addr
= NULL
;
866 ipstat
.ips_reassembled
++;
867 ip
= mtod(m
, struct ip
*);
868 /* Get the header length of the reassembled packet */
869 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
871 /* Restore original checksum before diverting packet */
872 if (divert_info
!= 0) {
877 ip
->ip_sum
= in_cksum(m
, hlen
);
891 * Divert or tee packet to the divert protocol if required.
893 * If divert_info is zero then cookie should be too, so we shouldn't
894 * need to clear them here. Assume divert_packet() does so also.
896 if (divert_info
!= 0) {
897 struct mbuf
*clone
= NULL
;
899 /* Clone packet if we're doing a 'tee' */
900 if ((divert_info
& IP_FW_PORT_TEE_FLAG
) != 0)
901 clone
= m_dup(m
, M_DONTWAIT
);
903 /* Restore packet header fields to original values */
908 /* Deliver packet to divert input routine */
909 ip_divert_cookie
= divert_cookie
;
910 divert_packet(m
, 1, divert_info
& 0xffff);
911 ipstat
.ips_delivered
++;
913 /* If 'tee', continue with original packet */
917 ip
= mtod(m
, struct ip
*);
923 * enforce IPsec policy checking if we are seeing last header.
924 * note that we do not visit this with protocols with pcb layer
925 * code - like udp/tcp/raw ip.
927 if (ipsec_bypass
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
) != 0 &&
928 ipsec4_in_reject(m
, NULL
)) {
929 ipsecstat
.in_polvio
++;
935 * Switch out to protocol's input routine.
937 ipstat
.ips_delivered
++;
939 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
940 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
942 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
);
943 #if IPFIREWALL_FORWARD
944 ip_fw_fwd_addr
= NULL
; /* tcp needed it */
949 #if IPFIREWALL_FORWARD
950 ip_fw_fwd_addr
= NULL
;
952 KERNEL_DEBUG(DBG_LAYER_END
, 0,0,0,0,0);
957 * IP software interrupt routine - to go away sometime soon
965 KERNEL_DEBUG(DBG_FNC_IP_INPUT
| DBG_FUNC_START
, 0,0,0,0,0);
969 IF_DEQUEUE(&ipintrq
, m
);
972 KERNEL_DEBUG(DBG_FNC_IP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
980 NETISR_SET(NETISR_IP
, ipintr
);
983 * Take incoming datagram fragment and try to reassemble it into
984 * whole datagram. If a chain for reassembly of this datagram already
985 * exists, then it is given as fp; otherwise have to make a chain.
987 * When IPDIVERT enabled, keep additional state with each packet that
988 * tells us if we need to divert or tee the packet we're building.
993 ip_reass(m
, fp
, where
, divinfo
, divcookie
)
995 ip_reass(m
, fp
, where
)
997 register struct mbuf
*m
;
998 register struct ipq
*fp
;
1006 u_int16_t
*divcookie
;
1009 struct ip
*ip
= mtod(m
, struct ip
*);
1010 register struct mbuf
*p
= 0, *q
, *nq
;
1012 int hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1016 * Presence of header sizes in mbufs
1017 * would confuse code below.
1022 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1023 m
->m_pkthdr
.csum_flags
= 0;
1025 * If first fragment to arrive, create a reassembly queue.
1029 * Enforce upper bound on number of fragmented packets
1030 * for which we attempt reassembly;
1031 * If maxfrag is 0, never accept fragments.
1032 * If maxfrag is -1, accept all fragments without limitation.
1034 if ((ip_maxfragpackets
>= 0) && (ip_nfragpackets
>= ip_maxfragpackets
))
1037 if ((t
= m_get(M_DONTWAIT
, MT_FTABLE
)) == NULL
)
1039 fp
= mtod(t
, struct ipq
*);
1040 insque((void*)fp
, (void*)where
);
1042 fp
->ipq_ttl
= IPFRAGTTL
;
1043 fp
->ipq_p
= ip
->ip_p
;
1044 fp
->ipq_id
= ip
->ip_id
;
1045 fp
->ipq_src
= ip
->ip_src
;
1046 fp
->ipq_dst
= ip
->ip_dst
;
1048 m
->m_nextpkt
= NULL
;
1051 fp
->ipq_div_info
= 0;
1055 fp
->ipq_div_cookie
= 0;
1060 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
1063 * Find a segment which begins after this one does.
1065 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
1066 if (GETIP(q
)->ip_off
> ip
->ip_off
)
1070 * If there is a preceding segment, it may provide some of
1071 * our data already. If so, drop the data from the incoming
1072 * segment. If it provides all of our data, drop us, otherwise
1073 * stick new segment in the proper place.
1075 * If some of the data is dropped from the the preceding
1076 * segment, then it's checksum is invalidated.
1079 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
1081 if (i
>= ip
->ip_len
)
1084 m
->m_pkthdr
.csum_flags
= 0;
1088 m
->m_nextpkt
= p
->m_nextpkt
;
1091 m
->m_nextpkt
= fp
->ipq_frags
;
1096 * While we overlap succeeding segments trim them or,
1097 * if they are completely covered, dequeue them.
1099 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
1101 i
= (ip
->ip_off
+ ip
->ip_len
) -
1103 if (i
< GETIP(q
)->ip_len
) {
1104 GETIP(q
)->ip_len
-= i
;
1105 GETIP(q
)->ip_off
+= i
;
1107 q
->m_pkthdr
.csum_flags
= 0;
1119 * Transfer firewall instructions to the fragment structure.
1120 * Any fragment diverting causes the whole packet to divert.
1123 fp
->ipq_div_info
= *divinfo
;
1125 fp
->ipq_divert
= *divinfo
;
1127 fp
->ipq_div_cookie
= *divcookie
;
1133 * Check for complete reassembly.
1136 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
) {
1137 if (GETIP(q
)->ip_off
!= next
)
1139 next
+= GETIP(q
)->ip_len
;
1141 /* Make sure the last packet didn't have the IP_MF flag */
1142 if (p
->m_flags
& M_FRAG
)
1146 * Reassembly is complete. Make sure the packet is a sane size.
1150 if (next
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) {
1151 ipstat
.ips_toolong
++;
1157 * Concatenate fragments.
1165 for (q
= nq
; q
!= NULL
; q
= nq
) {
1167 q
->m_nextpkt
= NULL
;
1168 if (q
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1169 m
->m_pkthdr
.csum_flags
= 0;
1171 m
->m_pkthdr
.csum_flags
&= q
->m_pkthdr
.csum_flags
;
1172 m
->m_pkthdr
.csum_data
+= q
->m_pkthdr
.csum_data
;
1179 * Extract firewall instructions from the fragment structure.
1182 *divinfo
= fp
->ipq_div_info
;
1184 *divinfo
= fp
->ipq_divert
;
1186 *divcookie
= fp
->ipq_div_cookie
;
1190 * Create header for new ip packet by
1191 * modifying header of first packet;
1192 * dequeue and discard fragment reassembly header.
1193 * Make header visible.
1196 ip
->ip_src
= fp
->ipq_src
;
1197 ip
->ip_dst
= fp
->ipq_dst
;
1200 (void) m_free(dtom(fp
));
1202 m
->m_len
+= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1203 m
->m_data
-= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1204 /* some debugging cruft by sklower, below, will go away soon */
1205 if (m
->m_flags
& M_PKTHDR
) { /* XXX this should be done elsewhere */
1206 register int plen
= 0;
1207 for (t
= m
; t
; t
= t
->m_next
)
1209 m
->m_pkthdr
.len
= plen
;
1218 ipstat
.ips_fragdropped
++;
1226 * Free a fragment reassembly header and all
1227 * associated datagrams.
1233 register struct mbuf
*q
;
1235 while (fp
->ipq_frags
) {
1237 fp
->ipq_frags
= q
->m_nextpkt
;
1241 (void) m_free(dtom(fp
));
1247 * IP timer processing;
1248 * if a timer expires on a reassembly
1249 * queue, discard it.
1254 register struct ipq
*fp
;
1258 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1262 while (fp
!= &ipq
[i
]) {
1265 if (fp
->prev
->ipq_ttl
== 0) {
1266 ipstat
.ips_fragtimeout
++;
1272 * If we are over the maximum number of fragments
1273 * (due to the limit being lowered), drain off
1274 * enough to get down to the new limit.
1276 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1277 if (ip_maxfragpackets
>= 0) {
1278 while ((ip_nfragpackets
> ip_maxfragpackets
) &&
1279 (ipq
[i
].next
!= &ipq
[i
])) {
1280 ipstat
.ips_fragdropped
++;
1281 ip_freef(ipq
[i
].next
);
1290 * Drain off all datagram fragments.
1297 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1298 while (ipq
[i
].next
!= &ipq
[i
]) {
1299 ipstat
.ips_fragdropped
++;
1300 ip_freef(ipq
[i
].next
);
1307 * Do option processing on a datagram,
1308 * possibly discarding it if bad options are encountered,
1309 * or forwarding it if source-routed.
1310 * Returns 1 if packet has been forwarded/freed,
1311 * 0 if the packet should be processed further.
1317 register struct ip
*ip
= mtod(m
, struct ip
*);
1318 register u_char
*cp
;
1319 register struct ip_timestamp
*ipt
;
1320 register struct in_ifaddr
*ia
;
1321 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
1322 struct in_addr
*sin
, dst
;
1326 cp
= (u_char
*)(ip
+ 1);
1327 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1328 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1329 opt
= cp
[IPOPT_OPTVAL
];
1330 if (opt
== IPOPT_EOL
)
1332 if (opt
== IPOPT_NOP
)
1335 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
)) {
1336 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1339 optlen
= cp
[IPOPT_OLEN
];
1340 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
) {
1341 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1351 * Source routing with record.
1352 * Find interface with current destination address.
1353 * If none on this machine then drop if strictly routed,
1354 * or do nothing if loosely routed.
1355 * Record interface address and bring up next address
1356 * component. If strictly routed make sure next
1357 * address is on directly accessible net.
1361 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1362 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1365 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1366 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1369 ipaddr
.sin_addr
= ip
->ip_dst
;
1370 ia
= (struct in_ifaddr
*)
1371 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
1373 if (opt
== IPOPT_SSRR
) {
1374 type
= ICMP_UNREACH
;
1375 code
= ICMP_UNREACH_SRCFAIL
;
1378 if (!ip_dosourceroute
)
1379 goto nosourcerouting
;
1381 * Loose routing, and not at next destination
1382 * yet; nothing to do except forward.
1386 off
--; /* 0 origin */
1387 if (off
> optlen
- (int)sizeof(struct in_addr
)) {
1389 * End of source route. Should be for us.
1391 if (!ip_acceptsourceroute
)
1392 goto nosourcerouting
;
1393 save_rte(cp
, ip
->ip_src
);
1397 if (!ip_dosourceroute
) {
1399 char buf
[16]; /* aaa.bbb.ccc.ddd\0 */
1401 * Acting as a router, so generate ICMP
1404 strcpy(buf
, inet_ntoa(ip
->ip_dst
));
1406 "attempted source route from %s to %s\n",
1407 inet_ntoa(ip
->ip_src
), buf
);
1408 type
= ICMP_UNREACH
;
1409 code
= ICMP_UNREACH_SRCFAIL
;
1413 * Not acting as a router, so silently drop.
1415 ipstat
.ips_cantforward
++;
1422 * locate outgoing interface
1424 (void)memcpy(&ipaddr
.sin_addr
, cp
+ off
,
1425 sizeof(ipaddr
.sin_addr
));
1427 if (opt
== IPOPT_SSRR
) {
1428 #define INA struct in_ifaddr *
1429 #define SA struct sockaddr *
1430 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0)
1431 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
1433 ia
= ip_rtaddr(ipaddr
.sin_addr
);
1435 type
= ICMP_UNREACH
;
1436 code
= ICMP_UNREACH_SRCFAIL
;
1439 ip
->ip_dst
= ipaddr
.sin_addr
;
1440 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1441 sizeof(struct in_addr
));
1442 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1444 * Let ip_intr's mcast routing check handle mcast pkts
1446 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
1450 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1451 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1454 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1455 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1459 * If no space remains, ignore.
1461 off
--; /* 0 origin */
1462 if (off
> optlen
- (int)sizeof(struct in_addr
))
1464 (void)memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
1465 sizeof(ipaddr
.sin_addr
));
1467 * locate outgoing interface; if we're the destination,
1468 * use the incoming interface (should be same).
1470 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0 &&
1471 (ia
= ip_rtaddr(ipaddr
.sin_addr
)) == 0) {
1472 type
= ICMP_UNREACH
;
1473 code
= ICMP_UNREACH_HOST
;
1476 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1477 sizeof(struct in_addr
));
1478 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1482 code
= cp
- (u_char
*)ip
;
1483 ipt
= (struct ip_timestamp
*)cp
;
1484 if (ipt
->ipt_len
< 4 || ipt
->ipt_len
> 40) {
1485 code
= (u_char
*)&ipt
->ipt_len
- (u_char
*)ip
;
1488 if (ipt
->ipt_ptr
< 5) {
1489 code
= (u_char
*)&ipt
->ipt_ptr
- (u_char
*)ip
;
1493 ipt
->ipt_len
- (int)sizeof(int32_t)) {
1494 if (++ipt
->ipt_oflw
== 0) {
1495 code
= (u_char
*)&ipt
->ipt_ptr
-
1501 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
1502 switch (ipt
->ipt_flg
) {
1504 case IPOPT_TS_TSONLY
:
1507 case IPOPT_TS_TSANDADDR
:
1508 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1509 sizeof(struct in_addr
) > ipt
->ipt_len
) {
1510 code
= (u_char
*)&ipt
->ipt_ptr
-
1514 ipaddr
.sin_addr
= dst
;
1515 ia
= (INA
)ifaof_ifpforaddr((SA
)&ipaddr
,
1519 (void)memcpy(sin
, &IA_SIN(ia
)->sin_addr
,
1520 sizeof(struct in_addr
));
1521 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1524 case IPOPT_TS_PRESPEC
:
1525 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1526 sizeof(struct in_addr
) > ipt
->ipt_len
) {
1527 code
= (u_char
*)&ipt
->ipt_ptr
-
1531 (void)memcpy(&ipaddr
.sin_addr
, sin
,
1532 sizeof(struct in_addr
));
1533 if (ifa_ifwithaddr((SA
)&ipaddr
) == 0)
1535 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1539 /* XXX can't take &ipt->ipt_flg */
1540 code
= (u_char
*)&ipt
->ipt_ptr
-
1545 (void)memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
1547 ipt
->ipt_ptr
+= sizeof(n_time
);
1550 if (forward
&& ipforwarding
) {
1556 ip
->ip_len
-= IP_VHL_HL(ip
->ip_vhl
) << 2; /* XXX icmp_error adds in hdr length */
1557 icmp_error(m
, type
, code
, 0, 0);
1558 ipstat
.ips_badoptions
++;
1563 * Given address of next destination (final or next hop),
1564 * return internet address info of interface to be used to get there.
1566 static struct in_ifaddr
*
1570 register struct sockaddr_in
*sin
;
1572 sin
= (struct sockaddr_in
*) &ipforward_rt
.ro_dst
;
1574 if (ipforward_rt
.ro_rt
== 0 || dst
.s_addr
!= sin
->sin_addr
.s_addr
||
1575 ipforward_rt
.ro_rt
->generation_id
!= route_generation
) {
1576 if (ipforward_rt
.ro_rt
) {
1577 rtfree(ipforward_rt
.ro_rt
);
1578 ipforward_rt
.ro_rt
= 0;
1580 sin
->sin_family
= AF_INET
;
1581 sin
->sin_len
= sizeof(*sin
);
1582 sin
->sin_addr
= dst
;
1584 rtalloc_ign(&ipforward_rt
, RTF_PRCLONING
);
1586 if (ipforward_rt
.ro_rt
== 0)
1587 return ((struct in_ifaddr
*)0);
1588 return ((struct in_ifaddr
*) ipforward_rt
.ro_rt
->rt_ifa
);
1592 * Save incoming source route for use in replies,
1593 * to be picked up later by ip_srcroute if the receiver is interested.
1596 save_rte(option
, dst
)
1602 olen
= option
[IPOPT_OLEN
];
1605 printf("save_rte: olen %d\n", olen
);
1607 if (olen
> sizeof(ip_srcrt
) - (1 + sizeof(dst
)))
1609 bcopy(option
, ip_srcrt
.srcopt
, olen
);
1610 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof(struct in_addr
);
1615 * Retrieve incoming source route for use in replies,
1616 * in the same form used by setsockopt.
1617 * The first hop is placed before the options, will be removed later.
1622 register struct in_addr
*p
, *q
;
1623 register struct mbuf
*m
;
1626 return ((struct mbuf
*)0);
1627 m
= m_get(M_DONTWAIT
, MT_HEADER
);
1629 return ((struct mbuf
*)0);
1631 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1633 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1634 m
->m_len
= ip_nhops
* sizeof(struct in_addr
) + sizeof(struct in_addr
) +
1638 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
1642 * First save first hop for return route
1644 p
= &ip_srcrt
.route
[ip_nhops
- 1];
1645 *(mtod(m
, struct in_addr
*)) = *p
--;
1648 printf(" hops %lx", (u_long
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
1652 * Copy option fields and padding (nop) to mbuf.
1654 ip_srcrt
.nop
= IPOPT_NOP
;
1655 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
1656 (void)memcpy(mtod(m
, caddr_t
) + sizeof(struct in_addr
),
1657 &ip_srcrt
.nop
, OPTSIZ
);
1658 q
= (struct in_addr
*)(mtod(m
, caddr_t
) +
1659 sizeof(struct in_addr
) + OPTSIZ
);
1662 * Record return path as an IP source route,
1663 * reversing the path (pointers are now aligned).
1665 while (p
>= ip_srcrt
.route
) {
1668 printf(" %lx", (u_long
)ntohl(q
->s_addr
));
1673 * Last hop goes to final destination.
1678 printf(" %lx\n", (u_long
)ntohl(q
->s_addr
));
1684 * Strip out IP options, at higher
1685 * level protocol in the kernel.
1686 * Second argument is buffer to which options
1687 * will be moved, and return value is their length.
1688 * XXX should be deleted; last arg currently ignored.
1691 ip_stripoptions(m
, mopt
)
1692 register struct mbuf
*m
;
1696 struct ip
*ip
= mtod(m
, struct ip
*);
1697 register caddr_t opts
;
1700 olen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1701 opts
= (caddr_t
)(ip
+ 1);
1702 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
1703 bcopy(opts
+ olen
, opts
, (unsigned)i
);
1705 if (m
->m_flags
& M_PKTHDR
)
1706 m
->m_pkthdr
.len
-= olen
;
1707 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
1710 u_char inetctlerrmap
[PRC_NCMDS
] = {
1712 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
1713 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
1714 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
1716 ENOPROTOOPT
, ECONNREFUSED
1720 * Forward a packet. If some error occurs return the sender
1721 * an icmp packet. Note we can't always generate a meaningful
1722 * icmp message because icmp doesn't have a large enough repertoire
1723 * of codes and types.
1725 * If not forwarding, just drop the packet. This could be confusing
1726 * if ipforwarding was zero but some routing protocol was advancing
1727 * us as a gateway to somewhere. However, we must let the routing
1728 * protocol deal with that.
1730 * The srcrt parameter indicates whether the packet is being forwarded
1731 * via a source route.
1734 ip_forward(m
, srcrt
)
1738 register struct ip
*ip
= mtod(m
, struct ip
*);
1739 register struct sockaddr_in
*sin
;
1740 register struct rtentry
*rt
;
1741 int error
, type
= 0, code
= 0;
1744 struct ifnet
*destifp
;
1746 struct ifnet dummyifp
;
1752 printf("forward: src %lx dst %lx ttl %x\n",
1753 (u_long
)ip
->ip_src
.s_addr
, (u_long
)ip
->ip_dst
.s_addr
,
1758 if (m
->m_flags
& (M_BCAST
|M_MCAST
) || in_canforward(ip
->ip_dst
) == 0) {
1759 ipstat
.ips_cantforward
++;
1766 if (ip
->ip_ttl
<= IPTTLDEC
) {
1767 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
,
1775 sin
= (struct sockaddr_in
*)&ipforward_rt
.ro_dst
;
1776 if ((rt
= ipforward_rt
.ro_rt
) == 0 ||
1777 ip
->ip_dst
.s_addr
!= sin
->sin_addr
.s_addr
||
1778 ipforward_rt
.ro_rt
->generation_id
!= route_generation
) {
1779 if (ipforward_rt
.ro_rt
) {
1780 rtfree(ipforward_rt
.ro_rt
);
1781 ipforward_rt
.ro_rt
= 0;
1783 sin
->sin_family
= AF_INET
;
1784 sin
->sin_len
= sizeof(*sin
);
1785 sin
->sin_addr
= ip
->ip_dst
;
1787 rtalloc_ign(&ipforward_rt
, RTF_PRCLONING
);
1788 if (ipforward_rt
.ro_rt
== 0) {
1789 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0);
1792 rt
= ipforward_rt
.ro_rt
;
1796 * Save the IP header and at most 8 bytes of the payload,
1797 * in case we need to generate an ICMP message to the src.
1799 * We don't use m_copy() because it might return a reference
1800 * to a shared cluster. Both this function and ip_output()
1801 * assume exclusive access to the IP header in `m', so any
1802 * data in a cluster may change before we reach icmp_error().
1804 MGET(mcopy
, M_DONTWAIT
, m
->m_type
);
1805 if (mcopy
!= NULL
) {
1806 M_COPY_PKTHDR(mcopy
, m
);
1807 mcopy
->m_len
= imin((IP_VHL_HL(ip
->ip_vhl
) << 2) + 8,
1809 m_copydata(m
, 0, mcopy
->m_len
, mtod(mcopy
, caddr_t
));
1815 ip
->ip_ttl
-= IPTTLDEC
;
1821 * If forwarding packet using same interface that it came in on,
1822 * perhaps should send a redirect to sender to shortcut a hop.
1823 * Only send redirect if source is sending directly to us,
1824 * and if packet was not source routed (or has any options).
1825 * Also, don't send redirect if forwarding using a default route
1826 * or a route modified by a redirect.
1828 #define satosin(sa) ((struct sockaddr_in *)(sa))
1829 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
1830 (rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) == 0 &&
1831 satosin(rt_key(rt
))->sin_addr
.s_addr
!= 0 &&
1832 ipsendredirects
&& !srcrt
) {
1833 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1834 u_long src
= ntohl(ip
->ip_src
.s_addr
);
1837 (src
& RTA(rt
)->ia_subnetmask
) == RTA(rt
)->ia_subnet
) {
1838 if (rt
->rt_flags
& RTF_GATEWAY
)
1839 dest
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
;
1841 dest
= ip
->ip_dst
.s_addr
;
1842 /* Router requirements says to only send host redirects */
1843 type
= ICMP_REDIRECT
;
1844 code
= ICMP_REDIRECT_HOST
;
1847 printf("redirect (%d) to %lx\n", code
, (u_long
)dest
);
1852 error
= ip_output(m
, (struct mbuf
*)0, &ipforward_rt
,
1855 ipstat
.ips_cantforward
++;
1857 ipstat
.ips_forward
++;
1859 ipstat
.ips_redirectsent
++;
1862 ipflow_create(&ipforward_rt
, mcopy
);
1874 case 0: /* forwarded, but need redirect */
1875 /* type, code set above */
1878 case ENETUNREACH
: /* shouldn't happen, checked above */
1883 type
= ICMP_UNREACH
;
1884 code
= ICMP_UNREACH_HOST
;
1888 type
= ICMP_UNREACH
;
1889 code
= ICMP_UNREACH_NEEDFRAG
;
1891 if (ipforward_rt
.ro_rt
)
1892 destifp
= ipforward_rt
.ro_rt
->rt_ifp
;
1895 * If the packet is routed over IPsec tunnel, tell the
1896 * originator the tunnel MTU.
1897 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1900 if (ipforward_rt
.ro_rt
) {
1901 struct secpolicy
*sp
= NULL
;
1907 destifp
= ipforward_rt
.ro_rt
->rt_ifp
;
1908 ipstat
.ips_cantfrag
++;
1912 sp
= ipsec4_getpolicybyaddr(mcopy
,
1918 destifp
= ipforward_rt
.ro_rt
->rt_ifp
;
1920 /* count IPsec header size */
1921 ipsechdr
= ipsec4_hdrsiz(mcopy
,
1926 * find the correct route for outer IPv4
1927 * header, compute tunnel MTU.
1930 * The "dummyifp" code relies upon the fact
1931 * that icmp_error() touches only ifp->if_mtu.
1936 && sp
->req
->sav
!= NULL
1937 && sp
->req
->sav
->sah
!= NULL
) {
1938 ro
= &sp
->req
->sav
->sah
->sa_route
;
1939 if (ro
->ro_rt
&& ro
->ro_rt
->rt_ifp
) {
1941 ro
->ro_rt
->rt_ifp
->if_mtu
;
1942 dummyifp
.if_mtu
-= ipsechdr
;
1943 destifp
= &dummyifp
;
1951 ipstat
.ips_cantfrag
++;
1955 type
= ICMP_SOURCEQUENCH
;
1959 case EACCES
: /* ipfw denied packet */
1963 icmp_error(mcopy
, type
, code
, dest
, destifp
);
1967 ip_savecontrol(inp
, mp
, ip
, m
)
1968 register struct inpcb
*inp
;
1969 register struct mbuf
**mp
;
1970 register struct ip
*ip
;
1971 register struct mbuf
*m
;
1973 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
1977 *mp
= sbcreatecontrol((caddr_t
) &tv
, sizeof(tv
),
1978 SCM_TIMESTAMP
, SOL_SOCKET
);
1980 mp
= &(*mp
)->m_next
;
1982 if (inp
->inp_flags
& INP_RECVDSTADDR
) {
1983 *mp
= sbcreatecontrol((caddr_t
) &ip
->ip_dst
,
1984 sizeof(struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
);
1986 mp
= &(*mp
)->m_next
;
1990 * Moving these out of udp_input() made them even more broken
1991 * than they already were.
1993 /* options were tossed already */
1994 if (inp
->inp_flags
& INP_RECVOPTS
) {
1995 *mp
= sbcreatecontrol((caddr_t
) opts_deleted_above
,
1996 sizeof(struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
);
1998 mp
= &(*mp
)->m_next
;
2000 /* ip_srcroute doesn't do what we want here, need to fix */
2001 if (inp
->inp_flags
& INP_RECVRETOPTS
) {
2002 *mp
= sbcreatecontrol((caddr_t
) ip_srcroute(),
2003 sizeof(struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
);
2005 mp
= &(*mp
)->m_next
;
2008 if (inp
->inp_flags
& INP_RECVIF
) {
2011 struct sockaddr_dl sdl
;
2014 struct sockaddr_dl
*sdp
;
2015 struct sockaddr_dl
*sdl2
= &sdlbuf
.sdl
;
2017 if (((ifp
= m
->m_pkthdr
.rcvif
))
2018 && ( ifp
->if_index
&& (ifp
->if_index
<= if_index
))) {
2019 sdp
= (struct sockaddr_dl
*)(ifnet_addrs
2020 [ifp
->if_index
- 1]->ifa_addr
);
2022 * Change our mind and don't try copy.
2024 if ((sdp
->sdl_family
!= AF_LINK
)
2025 || (sdp
->sdl_len
> sizeof(sdlbuf
))) {
2028 bcopy(sdp
, sdl2
, sdp
->sdl_len
);
2032 = offsetof(struct sockaddr_dl
, sdl_data
[0]);
2033 sdl2
->sdl_family
= AF_LINK
;
2034 sdl2
->sdl_index
= 0;
2035 sdl2
->sdl_nlen
= sdl2
->sdl_alen
= sdl2
->sdl_slen
= 0;
2037 *mp
= sbcreatecontrol((caddr_t
) sdl2
, sdl2
->sdl_len
,
2038 IP_RECVIF
, IPPROTO_IP
);
2040 mp
= &(*mp
)->m_next
;
2042 if (inp
->inp_flags
& INP_RECVTTL
) {
2043 *mp
= sbcreatecontrol((caddr_t
)&ip
->ip_ttl
, sizeof(ip
->ip_ttl
), IP_RECVTTL
, IPPROTO_IP
);
2044 if (*mp
) mp
= &(*mp
)->m_next
;
2049 ip_rsvp_init(struct socket
*so
)
2051 if (so
->so_type
!= SOCK_RAW
||
2052 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2055 if (ip_rsvpd
!= NULL
)
2060 * This may seem silly, but we need to be sure we don't over-increment
2061 * the RSVP counter, in case something slips up.
2076 * This may seem silly, but we need to be sure we don't over-decrement
2077 * the RSVP counter, in case something slips up.