2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_bootp.h"
37 #include "opt_ipstealth.h"
38 #include "opt_ipsec.h"
39 #include "opt_route.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/hhook.h>
46 #include <sys/malloc.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
51 #include <sys/kernel.h>
53 #include <sys/rmlock.h>
54 #include <sys/rwlock.h>
56 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
61 #include <net/if_types.h>
62 #include <net/if_var.h>
63 #include <net/if_dl.h>
64 #include <net/route.h>
65 #include <net/netisr.h>
66 #include <net/rss_config.h>
69 #include <netinet/in.h>
70 #include <netinet/in_kdtrace.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/in_var.h>
73 #include <netinet/ip.h>
74 #include <netinet/in_pcb.h>
75 #include <netinet/ip_var.h>
76 #include <netinet/ip_fw.h>
77 #include <netinet/ip_icmp.h>
78 #include <netinet/ip_options.h>
79 #include <machine/in_cksum.h>
80 #include <netinet/ip_carp.h>
82 #include <netinet/ip_ipsec.h>
83 #include <netipsec/ipsec.h>
84 #include <netipsec/key.h>
86 #include <netinet/in_rss.h>
88 #include <sys/socketvar.h>
90 #include <security/mac/mac_framework.h>
93 CTASSERT(sizeof(struct ip) == 20);
96 /* IP reassembly functions are defined in ip_reass.c. */
97 extern void ipreass_init(void);
98 extern void ipreass_drain(void);
99 extern void ipreass_slowtimo(void);
101 extern void ipreass_destroy(void);
104 struct rmlock in_ifaddr_lock;
105 RM_SYSINIT(in_ifaddr_lock, &in_ifaddr_lock, "in_ifaddr_lock");
107 VNET_DEFINE(int, rsvp_on);
109 VNET_DEFINE(int, ipforwarding);
110 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_VNET | CTLFLAG_RW,
111 &VNET_NAME(ipforwarding), 0,
112 "Enable IP forwarding between interfaces");
114 static VNET_DEFINE(int, ipsendredirects) = 1; /* XXX */
115 #define V_ipsendredirects VNET(ipsendredirects)
116 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_VNET | CTLFLAG_RW,
117 &VNET_NAME(ipsendredirects), 0,
118 "Enable sending IP redirects");
121 * XXX - Setting ip_checkinterface mostly implements the receive side of
122 * the Strong ES model described in RFC 1122, but since the routing table
123 * and transmit implementation do not implement the Strong ES model,
124 * setting this to 1 results in an odd hybrid.
126 * XXX - ip_checkinterface currently must be disabled if you use ipnat
127 * to translate the destination address to another local interface.
129 * XXX - ip_checkinterface must be disabled if you add IP aliases
130 * to the loopback interface instead of the interface where the
131 * packets for those addresses are received.
133 static VNET_DEFINE(int, ip_checkinterface);
134 #define V_ip_checkinterface VNET(ip_checkinterface)
135 SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_VNET | CTLFLAG_RW,
136 &VNET_NAME(ip_checkinterface), 0,
137 "Verify packet arrives on correct interface");
139 VNET_DEFINE(struct pfil_head, inet_pfil_hook); /* Packet filter hooks */
141 static struct netisr_handler ip_nh = {
143 .nh_handler = ip_input,
144 .nh_proto = NETISR_IP,
146 .nh_m2cpuid = rss_soft_m2cpuid_v4,
147 .nh_policy = NETISR_POLICY_CPU,
148 .nh_dispatch = NETISR_DISPATCH_HYBRID,
150 .nh_policy = NETISR_POLICY_FLOW,
156 * Directly dispatched frames are currently assumed
157 * to have a flowid already calculated.
159 * It should likely have something that assert it
160 * actually has valid flow details.
162 static struct netisr_handler ip_direct_nh = {
163 .nh_name = "ip_direct",
164 .nh_handler = ip_direct_input,
165 .nh_proto = NETISR_IP_DIRECT,
166 .nh_m2cpuid = rss_soft_m2cpuid_v4,
167 .nh_policy = NETISR_POLICY_CPU,
168 .nh_dispatch = NETISR_DISPATCH_HYBRID,
172 extern struct domain inetdomain;
173 extern struct protosw inetsw[];
174 u_char ip_protox[IPPROTO_MAX];
175 VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead); /* first inet address */
176 VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table */
177 VNET_DEFINE(u_long, in_ifaddrhmask); /* mask for hash table */
180 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
181 &ip_mtu, 0, "Default MTU");
185 VNET_DEFINE(int, ipstealth);
186 SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_VNET | CTLFLAG_RW,
187 &VNET_NAME(ipstealth), 0,
188 "IP stealth mode, no TTL decrementation on forwarding");
192 * IP statistics are stored in the "array" of counter(9)s.
194 VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat);
195 VNET_PCPUSTAT_SYSINIT(ipstat);
196 SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat,
197 "IP statistics (struct ipstat, netinet/ip_var.h)");
200 VNET_PCPUSTAT_SYSUNINIT(ipstat);
204 * Kernel module interface for updating ipstat. The argument is an index
205 * into ipstat treated as an array.
208 kmod_ipstat_inc(int statnum)
211 counter_u64_add(VNET(ipstat)[statnum], 1);
215 kmod_ipstat_dec(int statnum)
218 counter_u64_add(VNET(ipstat)[statnum], -1);
222 sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
226 netisr_getqlimit(&ip_nh, &qlimit);
227 error = sysctl_handle_int(oidp, &qlimit, 0, req);
228 if (error || !req->newptr)
232 return (netisr_setqlimit(&ip_nh, qlimit));
234 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen,
235 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_queue_maxlen, "I",
236 "Maximum size of the IP input queue");
239 sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
241 u_int64_t qdrops_long;
244 netisr_getqdrops(&ip_nh, &qdrops_long);
245 qdrops = qdrops_long;
246 error = sysctl_handle_int(oidp, &qdrops, 0, req);
247 if (error || !req->newptr)
251 netisr_clearqdrops(&ip_nh);
255 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops,
256 CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_queue_drops, "I",
257 "Number of packets dropped from the IP input queue");
261 sysctl_netinet_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
265 netisr_getqlimit(&ip_direct_nh, &qlimit);
266 error = sysctl_handle_int(oidp, &qlimit, 0, req);
267 if (error || !req->newptr)
271 return (netisr_setqlimit(&ip_direct_nh, qlimit));
273 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_direct_queue_maxlen,
274 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_direct_queue_maxlen, "I",
275 "Maximum size of the IP direct input queue");
278 sysctl_netinet_intr_direct_queue_drops(SYSCTL_HANDLER_ARGS)
280 u_int64_t qdrops_long;
283 netisr_getqdrops(&ip_direct_nh, &qdrops_long);
284 qdrops = qdrops_long;
285 error = sysctl_handle_int(oidp, &qdrops, 0, req);
286 if (error || !req->newptr)
290 netisr_clearqdrops(&ip_direct_nh);
294 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_direct_queue_drops,
295 CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_direct_queue_drops, "I",
296 "Number of packets dropped from the IP direct input queue");
300 * IP initialization: fill in IP protocol switch table.
301 * All protocols not implemented in kernel go to raw IP protocol handler.
309 TAILQ_INIT(&V_in_ifaddrhead);
310 V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
312 /* Initialize IP reassembly queue. */
315 /* Initialize packet filter hooks. */
316 V_inet_pfil_hook.ph_type = PFIL_TYPE_AF;
317 V_inet_pfil_hook.ph_af = AF_INET;
318 if ((i = pfil_head_register(&V_inet_pfil_hook)) != 0)
319 printf("%s: WARNING: unable to register pfil hook, "
320 "error %d\n", __func__, i);
322 if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET,
323 &V_ipsec_hhh_in[HHOOK_IPSEC_INET],
324 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
325 printf("%s: WARNING: unable to register input helper hook\n",
327 if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET,
328 &V_ipsec_hhh_out[HHOOK_IPSEC_INET],
329 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
330 printf("%s: WARNING: unable to register output helper hook\n",
333 /* Skip initialization of globals for non-default instances. */
335 if (!IS_DEFAULT_VNET(curvnet)) {
336 netisr_register_vnet(&ip_nh);
338 netisr_register_vnet(&ip_direct_nh);
344 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
346 panic("ip_init: PF_INET not found");
348 /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
349 for (i = 0; i < IPPROTO_MAX; i++)
350 ip_protox[i] = pr - inetsw;
352 * Cycle through IP protocols and put them into the appropriate place
355 for (pr = inetdomain.dom_protosw;
356 pr < inetdomain.dom_protoswNPROTOSW; pr++)
357 if (pr->pr_domain->dom_family == PF_INET &&
358 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
359 /* Be careful to only index valid IP protocols. */
360 if (pr->pr_protocol < IPPROTO_MAX)
361 ip_protox[pr->pr_protocol] = pr - inetsw;
364 netisr_register(&ip_nh);
366 netisr_register(&ip_direct_nh);
372 ip_destroy(void *unused __unused)
377 netisr_unregister_vnet(&ip_direct_nh);
379 netisr_unregister_vnet(&ip_nh);
381 if ((error = pfil_head_unregister(&V_inet_pfil_hook)) != 0)
382 printf("%s: WARNING: unable to unregister pfil hook, "
383 "error %d\n", __func__, error);
385 error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET]);
387 printf("%s: WARNING: unable to deregister input helper hook "
388 "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET: "
389 "error %d returned\n", __func__, error);
391 error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET]);
393 printf("%s: WARNING: unable to deregister output helper hook "
394 "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET: "
395 "error %d returned\n", __func__, error);
397 /* Cleanup in_ifaddr hash table; should be empty. */
398 hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask);
400 /* Destroy IP reassembly queue. */
404 VNET_SYSUNINIT(ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip_destroy, NULL);
409 * IP direct input routine.
411 * This is called when reinjecting completed fragments where
412 * all of the previous checking and book-keeping has been done.
415 ip_direct_input(struct mbuf *m)
420 ip = mtod(m, struct ip *);
421 hlen = ip->ip_hl << 2;
423 IPSTAT_INC(ips_delivered);
424 (*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p);
430 * Ip input routine. Checksum and byte swap header. If fragmented
431 * try to reassemble. Process options. Pass to next level.
434 ip_input(struct mbuf *m)
436 struct ip *ip = NULL;
437 struct in_ifaddr *ia = NULL;
440 int checkif, hlen = 0;
441 uint16_t sum, ip_len;
442 int dchg = 0; /* dest changed after fw */
443 struct in_addr odst; /* original dst address */
447 if (m->m_flags & M_FASTFWD_OURS) {
448 m->m_flags &= ~M_FASTFWD_OURS;
449 /* Set up some basics that will be used later. */
450 ip = mtod(m, struct ip *);
451 hlen = ip->ip_hl << 2;
452 ip_len = ntohs(ip->ip_len);
456 IPSTAT_INC(ips_total);
458 if (m->m_pkthdr.len < sizeof(struct ip))
461 if (m->m_len < sizeof (struct ip) &&
462 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
463 IPSTAT_INC(ips_toosmall);
466 ip = mtod(m, struct ip *);
468 if (ip->ip_v != IPVERSION) {
469 IPSTAT_INC(ips_badvers);
473 hlen = ip->ip_hl << 2;
474 if (hlen < sizeof(struct ip)) { /* minimum header length */
475 IPSTAT_INC(ips_badhlen);
478 if (hlen > m->m_len) {
479 if ((m = m_pullup(m, hlen)) == NULL) {
480 IPSTAT_INC(ips_badhlen);
483 ip = mtod(m, struct ip *);
486 IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL);
488 /* 127/8 must not appear on wire - RFC1122 */
489 ifp = m->m_pkthdr.rcvif;
490 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
491 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
492 if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
493 IPSTAT_INC(ips_badaddr);
498 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
499 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
501 if (hlen == sizeof(struct ip)) {
502 sum = in_cksum_hdr(ip);
504 sum = in_cksum(m, hlen);
508 IPSTAT_INC(ips_badsum);
513 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
514 /* packet is dropped by traffic conditioner */
518 ip_len = ntohs(ip->ip_len);
520 IPSTAT_INC(ips_badlen);
525 * Check that the amount of data in the buffers
526 * is as at least much as the IP header would have us expect.
527 * Trim mbufs if longer than we expect.
528 * Drop packet if shorter than we expect.
530 if (m->m_pkthdr.len < ip_len) {
532 IPSTAT_INC(ips_tooshort);
535 if (m->m_pkthdr.len > ip_len) {
536 if (m->m_len == m->m_pkthdr.len) {
538 m->m_pkthdr.len = ip_len;
540 m_adj(m, ip_len - m->m_pkthdr.len);
543 /* Try to forward the packet, but if we fail continue */
545 /* For now we do not handle IPSEC in tryforward. */
546 if (!key_havesp(IPSEC_DIR_INBOUND) && !key_havesp(IPSEC_DIR_OUTBOUND) &&
547 (V_ipforwarding == 1))
548 if (ip_tryforward(m) == NULL)
551 * Bypass packet filtering for packets previously handled by IPsec.
553 if (ip_ipsec_filtertunnel(m))
556 if (V_ipforwarding == 1)
557 if (ip_tryforward(m) == NULL)
562 * Run through list of hooks for input packets.
564 * NB: Beware of the destination address changing (e.g.
565 * by NAT rewriting). When this happens, tell
566 * ip_forward to do the right thing.
569 /* Jump over all PFIL processing if hooks are not active. */
570 if (!PFIL_HOOKED(&V_inet_pfil_hook))
574 if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0)
576 if (m == NULL) /* consumed by filter */
579 ip = mtod(m, struct ip *);
580 dchg = (odst.s_addr != ip->ip_dst.s_addr);
581 ifp = m->m_pkthdr.rcvif;
583 if (m->m_flags & M_FASTFWD_OURS) {
584 m->m_flags &= ~M_FASTFWD_OURS;
587 if (m->m_flags & M_IP_NEXTHOP) {
588 if (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
590 * Directly ship the packet on. This allows
591 * forwarding packets originally destined to us
592 * to some other directly connected host.
601 * Process options and, if not destined for us,
602 * ship it on. ip_dooptions returns 1 when an
603 * error was detected (causing an icmp message
604 * to be sent and the original packet to be freed).
606 if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
609 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
610 * matter if it is destined to another node, or whether it is
611 * a multicast one, RSVP wants it! and prevents it from being forwarded
612 * anywhere else. Also checks if the rsvp daemon is running before
613 * grabbing the packet.
615 if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
619 * Check our list of addresses, to see if the packet is for us.
620 * If we don't have any addresses, assume any unicast packet
621 * we receive might be for us (and let the upper layers deal
624 if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
625 (m->m_flags & (M_MCAST|M_BCAST)) == 0)
629 * Enable a consistency check between the destination address
630 * and the arrival interface for a unicast packet (the RFC 1122
631 * strong ES model) if IP forwarding is disabled and the packet
632 * is not locally generated and the packet is not subject to
635 * XXX - Checking also should be disabled if the destination
636 * address is ipnat'ed to a different interface.
638 * XXX - Checking is incompatible with IP aliases added
639 * to the loopback interface instead of the interface where
640 * the packets are received.
642 * XXX - This is the case for carp vhost IPs as well so we
643 * insert a workaround. If the packet got here, we already
644 * checked with carp_iamatch() and carp_forus().
646 checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
647 ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
648 ifp->if_carp == NULL && (dchg == 0);
651 * Check for exact addresses in the hash bucket.
653 /* IN_IFADDR_RLOCK(); */
654 LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
656 * If the address matches, verify that the packet
657 * arrived via the correct interface if checking is
660 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
661 (!checkif || ia->ia_ifp == ifp)) {
662 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
663 counter_u64_add(ia->ia_ifa.ifa_ibytes,
665 /* IN_IFADDR_RUNLOCK(); */
669 /* IN_IFADDR_RUNLOCK(); */
672 * Check for broadcast addresses.
674 * Only accept broadcast packets that arrive via the matching
675 * interface. Reception of forwarded directed broadcasts would
676 * be handled via ip_forward() and ether_output() with the loopback
677 * into the stack for SIMPLEX interfaces handled by ether_output().
679 if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
681 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
682 if (ifa->ifa_addr->sa_family != AF_INET)
685 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
687 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
688 counter_u64_add(ia->ia_ifa.ifa_ibytes,
690 IF_ADDR_RUNLOCK(ifp);
694 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
695 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
696 counter_u64_add(ia->ia_ifa.ifa_ibytes,
698 IF_ADDR_RUNLOCK(ifp);
703 IF_ADDR_RUNLOCK(ifp);
706 /* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
707 if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
708 IPSTAT_INC(ips_cantforward);
712 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
715 * If we are acting as a multicast router, all
716 * incoming multicast packets are passed to the
717 * kernel-level multicast forwarding function.
718 * The packet is returned (relatively) intact; if
719 * ip_mforward() returns a non-zero value, the packet
720 * must be discarded, else it may be accepted below.
722 if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
723 IPSTAT_INC(ips_cantforward);
729 * The process-level routing daemon needs to receive
730 * all multicast IGMP packets, whether or not this
731 * host belongs to their destination groups.
733 if (ip->ip_p == IPPROTO_IGMP)
735 IPSTAT_INC(ips_forward);
738 * Assume the packet is for us, to avoid prematurely taking
739 * a lock on the in_multi hash. Protocols must perform
740 * their own filtering and update statistics accordingly.
744 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
746 if (ip->ip_dst.s_addr == INADDR_ANY)
750 * Not for us; forward if possible and desirable.
752 if (V_ipforwarding == 0) {
753 IPSTAT_INC(ips_cantforward);
763 * IPSTEALTH: Process non-routing options only
764 * if the packet is destined for us.
766 if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1))
768 #endif /* IPSTEALTH */
771 * Attempt reassembly; if it succeeds, proceed.
772 * ip_reass() will return a different mbuf.
774 if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
775 /* XXXGL: shouldn't we save & set m_flags? */
779 ip = mtod(m, struct ip *);
780 /* Get the header length of the reassembled packet */
781 hlen = ip->ip_hl << 2;
786 * enforce IPsec policy checking if we are seeing last header.
787 * note that we do not visit this with protocols with pcb layer
788 * code - like udp/tcp/raw ip.
790 if (ip_ipsec_input(m, ip->ip_p) != 0)
795 * Switch out to protocol's input routine.
797 IPSTAT_INC(ips_delivered);
799 (*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p);
806 * IP timer processing;
807 * if a timer expires on a reassembly
813 VNET_ITERATOR_DECL(vnet_iter);
815 VNET_LIST_RLOCK_NOSLEEP();
816 VNET_FOREACH(vnet_iter) {
817 CURVNET_SET(vnet_iter);
821 VNET_LIST_RUNLOCK_NOSLEEP();
827 VNET_ITERATOR_DECL(vnet_iter);
829 VNET_LIST_RLOCK_NOSLEEP();
830 VNET_FOREACH(vnet_iter) {
831 CURVNET_SET(vnet_iter);
835 VNET_LIST_RUNLOCK_NOSLEEP();
839 * The protocol to be inserted into ip_protox[] must be already registered
840 * in inetsw[], either statically or through pf_proto_register().
843 ipproto_register(short ipproto)
848 if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
849 return (EPROTONOSUPPORT);
852 * The protocol slot must not be occupied by another protocol
853 * already. An index pointing to IPPROTO_RAW is unused.
855 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
857 return (EPFNOSUPPORT);
858 if (ip_protox[ipproto] != pr - inetsw) /* IPPROTO_RAW */
861 /* Find the protocol position in inetsw[] and set the index. */
862 for (pr = inetdomain.dom_protosw;
863 pr < inetdomain.dom_protoswNPROTOSW; pr++) {
864 if (pr->pr_domain->dom_family == PF_INET &&
865 pr->pr_protocol && pr->pr_protocol == ipproto) {
866 ip_protox[pr->pr_protocol] = pr - inetsw;
870 return (EPROTONOSUPPORT);
874 ipproto_unregister(short ipproto)
879 if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
880 return (EPROTONOSUPPORT);
882 /* Check if the protocol was indeed registered. */
883 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
885 return (EPFNOSUPPORT);
886 if (ip_protox[ipproto] == pr - inetsw) /* IPPROTO_RAW */
889 /* Reset the protocol slot to IPPROTO_RAW. */
890 ip_protox[ipproto] = pr - inetsw;
894 u_char inetctlerrmap[PRC_NCMDS] = {
896 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
897 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
898 EMSGSIZE, EHOSTUNREACH, 0, 0,
899 0, 0, EHOSTUNREACH, 0,
900 ENOPROTOOPT, ECONNREFUSED
904 * Forward a packet. If some error occurs return the sender
905 * an icmp packet. Note we can't always generate a meaningful
906 * icmp message because icmp doesn't have a large enough repertoire
907 * of codes and types.
909 * If not forwarding, just drop the packet. This could be confusing
910 * if ipforwarding was zero but some routing protocol was advancing
911 * us as a gateway to somewhere. However, we must let the routing
912 * protocol deal with that.
914 * The srcrt parameter indicates whether the packet is being forwarded
915 * via a source route.
918 ip_forward(struct mbuf *m, int srcrt)
920 struct ip *ip = mtod(m, struct ip *);
921 struct in_ifaddr *ia;
923 struct sockaddr_in *sin;
926 int error, type = 0, code = 0, mtu = 0;
928 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
929 IPSTAT_INC(ips_cantforward);
934 if (ip_ipsec_fwd(m) != 0) {
935 IPSTAT_INC(ips_cantforward);
943 if (ip->ip_ttl <= IPTTLDEC) {
944 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
952 bzero(&ro, sizeof(ro));
953 sin = (struct sockaddr_in *)&ro.ro_dst;
954 sin->sin_family = AF_INET;
955 sin->sin_len = sizeof(*sin);
956 sin->sin_addr = ip->ip_dst;
958 rtalloc_mpath_fib(&ro,
959 ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
962 in_rtalloc_ign(&ro, 0, M_GETFIB(m));
964 if (ro.ro_rt != NULL) {
965 ia = ifatoia(ro.ro_rt->rt_ifa);
966 ifa_ref(&ia->ia_ifa);
971 * 'ia' may be NULL if there is no route for this destination.
972 * In case of IPsec, Don't discard it just yet, but pass it to
973 * ip_output in case of outgoing IPsec policy.
975 if (!srcrt && ia == NULL) {
976 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
983 * Save the IP header and at most 8 bytes of the payload,
984 * in case we need to generate an ICMP message to the src.
986 * XXX this can be optimized a lot by saving the data in a local
987 * buffer on the stack (72 bytes at most), and only allocating the
988 * mbuf if really necessary. The vast majority of the packets
989 * are forwarded without having to send an ICMP back (either
990 * because unnecessary, or because rate limited), so we are
991 * really we are wasting a lot of work here.
993 * We don't use m_copy() because it might return a reference
994 * to a shared cluster. Both this function and ip_output()
995 * assume exclusive access to the IP header in `m', so any
996 * data in a cluster may change before we reach icmp_error().
998 mcopy = m_gethdr(M_NOWAIT, m->m_type);
999 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
1001 * It's probably ok if the pkthdr dup fails (because
1002 * the deep copy of the tag chain failed), but for now
1003 * be conservative and just discard the copy since
1004 * code below may some day want the tags.
1009 if (mcopy != NULL) {
1010 mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy));
1011 mcopy->m_pkthdr.len = mcopy->m_len;
1012 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1018 ip->ip_ttl -= IPTTLDEC;
1024 * If forwarding packet using same interface that it came in on,
1025 * perhaps should send a redirect to sender to shortcut a hop.
1026 * Only send redirect if source is sending directly to us,
1027 * and if packet was not source routed (or has any options).
1028 * Also, don't send redirect if forwarding using a default route
1029 * or a route modified by a redirect.
1032 if (!srcrt && V_ipsendredirects &&
1033 ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
1038 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1039 satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1040 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1041 u_long src = ntohl(ip->ip_src.s_addr);
1044 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1045 if (rt->rt_flags & RTF_GATEWAY)
1046 dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1048 dest.s_addr = ip->ip_dst.s_addr;
1049 /* Router requirements says to only send host redirects */
1050 type = ICMP_REDIRECT;
1051 code = ICMP_REDIRECT_HOST;
1056 error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1058 if (error == EMSGSIZE && ro.ro_rt)
1059 mtu = ro.ro_rt->rt_mtu;
1063 IPSTAT_INC(ips_cantforward);
1065 IPSTAT_INC(ips_forward);
1067 IPSTAT_INC(ips_redirectsent);
1072 ifa_free(&ia->ia_ifa);
1076 if (mcopy == NULL) {
1078 ifa_free(&ia->ia_ifa);
1084 case 0: /* forwarded, but need redirect */
1085 /* type, code set above */
1093 type = ICMP_UNREACH;
1094 code = ICMP_UNREACH_HOST;
1098 type = ICMP_UNREACH;
1099 code = ICMP_UNREACH_NEEDFRAG;
1103 * If IPsec is configured for this path,
1104 * override any possibly mtu value set by ip_output.
1106 mtu = ip_ipsec_mtu(mcopy, mtu);
1109 * If the MTU was set before make sure we are below the
1111 * If the MTU wasn't set before use the interface mtu or
1112 * fall back to the next smaller mtu step compared to the
1113 * current packet size.
1117 mtu = min(mtu, ia->ia_ifp->if_mtu);
1120 mtu = ia->ia_ifp->if_mtu;
1122 mtu = ip_next_mtu(ntohs(ip->ip_len), 0);
1124 IPSTAT_INC(ips_cantfrag);
1128 case EACCES: /* ipfw denied packet */
1131 ifa_free(&ia->ia_ifa);
1135 ifa_free(&ia->ia_ifa);
1136 icmp_error(mcopy, type, code, dest.s_addr, mtu);
1140 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1144 if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1148 if (inp->inp_socket->so_options & SO_BINTIME) {
1149 *mp = sbcreatecontrol((caddr_t)&bt, sizeof(bt),
1150 SCM_BINTIME, SOL_SOCKET);
1152 mp = &(*mp)->m_next;
1154 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1157 bintime2timeval(&bt, &tv);
1158 *mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv),
1159 SCM_TIMESTAMP, SOL_SOCKET);
1161 mp = &(*mp)->m_next;
1164 if (inp->inp_flags & INP_RECVDSTADDR) {
1165 *mp = sbcreatecontrol((caddr_t)&ip->ip_dst,
1166 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1168 mp = &(*mp)->m_next;
1170 if (inp->inp_flags & INP_RECVTTL) {
1171 *mp = sbcreatecontrol((caddr_t)&ip->ip_ttl,
1172 sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
1174 mp = &(*mp)->m_next;
1178 * Moving these out of udp_input() made them even more broken
1179 * than they already were.
1181 /* options were tossed already */
1182 if (inp->inp_flags & INP_RECVOPTS) {
1183 *mp = sbcreatecontrol((caddr_t)opts_deleted_above,
1184 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1186 mp = &(*mp)->m_next;
1188 /* ip_srcroute doesn't do what we want here, need to fix */
1189 if (inp->inp_flags & INP_RECVRETOPTS) {
1190 *mp = sbcreatecontrol((caddr_t)ip_srcroute(m),
1191 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1193 mp = &(*mp)->m_next;
1196 if (inp->inp_flags & INP_RECVIF) {
1199 struct sockaddr_dl sdl;
1202 struct sockaddr_dl *sdp;
1203 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1205 if ((ifp = m->m_pkthdr.rcvif) &&
1206 ifp->if_index && ifp->if_index <= V_if_index) {
1207 sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1209 * Change our mind and don't try copy.
1211 if (sdp->sdl_family != AF_LINK ||
1212 sdp->sdl_len > sizeof(sdlbuf)) {
1215 bcopy(sdp, sdl2, sdp->sdl_len);
1219 offsetof(struct sockaddr_dl, sdl_data[0]);
1220 sdl2->sdl_family = AF_LINK;
1221 sdl2->sdl_index = 0;
1222 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1224 *mp = sbcreatecontrol((caddr_t)sdl2, sdl2->sdl_len,
1225 IP_RECVIF, IPPROTO_IP);
1227 mp = &(*mp)->m_next;
1229 if (inp->inp_flags & INP_RECVTOS) {
1230 *mp = sbcreatecontrol((caddr_t)&ip->ip_tos,
1231 sizeof(u_char), IP_RECVTOS, IPPROTO_IP);
1233 mp = &(*mp)->m_next;
1236 if (inp->inp_flags2 & INP_RECVFLOWID) {
1237 uint32_t flowid, flow_type;
1239 flowid = m->m_pkthdr.flowid;
1240 flow_type = M_HASHTYPE_GET(m);
1243 * XXX should handle the failure of one or the
1244 * other - don't populate both?
1246 *mp = sbcreatecontrol((caddr_t) &flowid,
1247 sizeof(uint32_t), IP_FLOWID, IPPROTO_IP);
1249 mp = &(*mp)->m_next;
1250 *mp = sbcreatecontrol((caddr_t) &flow_type,
1251 sizeof(uint32_t), IP_FLOWTYPE, IPPROTO_IP);
1253 mp = &(*mp)->m_next;
1257 if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
1258 uint32_t flowid, flow_type;
1259 uint32_t rss_bucketid;
1261 flowid = m->m_pkthdr.flowid;
1262 flow_type = M_HASHTYPE_GET(m);
1264 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1265 *mp = sbcreatecontrol((caddr_t) &rss_bucketid,
1266 sizeof(uint32_t), IP_RSSBUCKETID, IPPROTO_IP);
1268 mp = &(*mp)->m_next;
1275 * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
1276 * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
1277 * locking. This code remains in ip_input.c as ip_mroute.c is optionally
1280 static VNET_DEFINE(int, ip_rsvp_on);
1281 VNET_DEFINE(struct socket *, ip_rsvpd);
1283 #define V_ip_rsvp_on VNET(ip_rsvp_on)
1286 ip_rsvp_init(struct socket *so)
1289 if (so->so_type != SOCK_RAW ||
1290 so->so_proto->pr_protocol != IPPROTO_RSVP)
1293 if (V_ip_rsvpd != NULL)
1298 * This may seem silly, but we need to be sure we don't over-increment
1299 * the RSVP counter, in case something slips up.
1301 if (!V_ip_rsvp_on) {
1315 * This may seem silly, but we need to be sure we don't over-decrement
1316 * the RSVP counter, in case something slips up.
1326 rsvp_input(struct mbuf **mp, int *offp, int proto)
1333 if (rsvp_input_p) { /* call the real one if loaded */
1335 rsvp_input_p(mp, offp, proto);
1336 return (IPPROTO_DONE);
1339 /* Can still get packets with rsvp_on = 0 if there is a local member
1340 * of the group to which the RSVP packet is addressed. But in this
1341 * case we want to throw the packet away.
1346 return (IPPROTO_DONE);
1349 if (V_ip_rsvpd != NULL) {
1351 rip_input(mp, offp, proto);
1352 return (IPPROTO_DONE);
1354 /* Drop the packet */
1356 return (IPPROTO_DONE);