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>
45 #include <sys/malloc.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
50 #include <sys/kernel.h>
52 #include <sys/rwlock.h>
54 #include <sys/syslog.h>
55 #include <sys/sysctl.h>
59 #include <net/if_types.h>
60 #include <net/if_var.h>
61 #include <net/if_dl.h>
62 #include <net/route.h>
63 #include <net/netisr.h>
64 #include <net/rss_config.h>
67 #include <netinet/in.h>
68 #include <netinet/in_kdtrace.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip.h>
72 #include <netinet/in_pcb.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/ip_fw.h>
75 #include <netinet/ip_icmp.h>
76 #include <netinet/ip_options.h>
77 #include <machine/in_cksum.h>
78 #include <netinet/ip_carp.h>
80 #include <netinet/ip_ipsec.h>
82 #include <netinet/in_rss.h>
84 #include <sys/socketvar.h>
86 #include <security/mac/mac_framework.h>
89 CTASSERT(sizeof(struct ip) == 20);
92 struct rwlock in_ifaddr_lock;
93 RW_SYSINIT(in_ifaddr_lock, &in_ifaddr_lock, "in_ifaddr_lock");
95 VNET_DEFINE(int, rsvp_on);
97 VNET_DEFINE(int, ipforwarding);
98 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_VNET | CTLFLAG_RW,
99 &VNET_NAME(ipforwarding), 0,
100 "Enable IP forwarding between interfaces");
102 static VNET_DEFINE(int, ipsendredirects) = 1; /* XXX */
103 #define V_ipsendredirects VNET(ipsendredirects)
104 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_VNET | CTLFLAG_RW,
105 &VNET_NAME(ipsendredirects), 0,
106 "Enable sending IP redirects");
109 * XXX - Setting ip_checkinterface mostly implements the receive side of
110 * the Strong ES model described in RFC 1122, but since the routing table
111 * and transmit implementation do not implement the Strong ES model,
112 * setting this to 1 results in an odd hybrid.
114 * XXX - ip_checkinterface currently must be disabled if you use ipnat
115 * to translate the destination address to another local interface.
117 * XXX - ip_checkinterface must be disabled if you add IP aliases
118 * to the loopback interface instead of the interface where the
119 * packets for those addresses are received.
121 static VNET_DEFINE(int, ip_checkinterface);
122 #define V_ip_checkinterface VNET(ip_checkinterface)
123 SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_VNET | CTLFLAG_RW,
124 &VNET_NAME(ip_checkinterface), 0,
125 "Verify packet arrives on correct interface");
127 VNET_DEFINE(struct pfil_head, inet_pfil_hook); /* Packet filter hooks */
129 static struct netisr_handler ip_nh = {
131 .nh_handler = ip_input,
132 .nh_proto = NETISR_IP,
134 .nh_m2cpuid = rss_soft_m2cpuid,
135 .nh_policy = NETISR_POLICY_CPU,
136 .nh_dispatch = NETISR_DISPATCH_HYBRID,
138 .nh_policy = NETISR_POLICY_FLOW,
144 * Directly dispatched frames are currently assumed
145 * to have a flowid already calculated.
147 * It should likely have something that assert it
148 * actually has valid flow details.
150 static struct netisr_handler ip_direct_nh = {
151 .nh_name = "ip_direct",
152 .nh_handler = ip_direct_input,
153 .nh_proto = NETISR_IP_DIRECT,
154 .nh_m2cpuid = rss_m2cpuid,
155 .nh_policy = NETISR_POLICY_CPU,
156 .nh_dispatch = NETISR_DISPATCH_HYBRID,
160 extern struct domain inetdomain;
161 extern struct protosw inetsw[];
162 u_char ip_protox[IPPROTO_MAX];
163 VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead); /* first inet address */
164 VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table */
165 VNET_DEFINE(u_long, in_ifaddrhmask); /* mask for hash table */
167 static VNET_DEFINE(uma_zone_t, ipq_zone);
168 static VNET_DEFINE(TAILQ_HEAD(ipqhead, ipq), ipq[IPREASS_NHASH]);
169 static struct mtx_padalign ipqlock[IPREASS_NHASH];
171 #define V_ipq_zone VNET(ipq_zone)
172 #define V_ipq VNET(ipq)
175 * The ipqlock array is global, /not/ per-VNET.
177 #define IPQ_LOCK(i) mtx_lock(&ipqlock[(i)])
178 #define IPQ_UNLOCK(i) mtx_unlock(&ipqlock[(i)])
179 #define IPQ_LOCK_INIT(i) mtx_init(&ipqlock[(i)], "ipqlock", NULL, MTX_DEF)
181 static void maxnipq_update(void);
182 static void ipq_zone_change(void *);
183 static void ip_drain_vnet(void);
184 static void ipq_free(struct ipqhead *, struct ipq *);
187 ipq_timeout(struct ipqhead *head, struct ipq *fp)
190 IPSTAT_ADD(ips_fragtimeout, fp->ipq_nfrags);
195 ipq_drop(struct ipqhead *head, struct ipq *fp)
198 IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
202 static VNET_DEFINE(int, maxnipq); /* Administrative limit on # reass queues. */
203 static VNET_DEFINE(int, nipq); /* Total # of reass queues */
204 #define V_maxnipq VNET(maxnipq)
205 #define V_nipq VNET(nipq)
206 SYSCTL_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_VNET | CTLFLAG_RD,
208 "Current number of IPv4 fragment reassembly queue entries");
210 static VNET_DEFINE(int, maxfragsperpacket);
211 #define V_maxfragsperpacket VNET(maxfragsperpacket)
212 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_VNET | CTLFLAG_RW,
213 &VNET_NAME(maxfragsperpacket), 0,
214 "Maximum number of IPv4 fragments allowed per packet");
217 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
218 &ip_mtu, 0, "Default MTU");
222 VNET_DEFINE(int, ipstealth);
223 SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_VNET | CTLFLAG_RW,
224 &VNET_NAME(ipstealth), 0,
225 "IP stealth mode, no TTL decrementation on forwarding");
229 * IP statistics are stored in the "array" of counter(9)s.
231 VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat);
232 VNET_PCPUSTAT_SYSINIT(ipstat);
233 SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat,
234 "IP statistics (struct ipstat, netinet/ip_var.h)");
237 VNET_PCPUSTAT_SYSUNINIT(ipstat);
241 * Kernel module interface for updating ipstat. The argument is an index
242 * into ipstat treated as an array.
245 kmod_ipstat_inc(int statnum)
248 counter_u64_add(VNET(ipstat)[statnum], 1);
252 kmod_ipstat_dec(int statnum)
255 counter_u64_add(VNET(ipstat)[statnum], -1);
259 sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
263 netisr_getqlimit(&ip_nh, &qlimit);
264 error = sysctl_handle_int(oidp, &qlimit, 0, req);
265 if (error || !req->newptr)
269 return (netisr_setqlimit(&ip_nh, qlimit));
271 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen,
272 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_queue_maxlen, "I",
273 "Maximum size of the IP input queue");
276 sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
278 u_int64_t qdrops_long;
281 netisr_getqdrops(&ip_nh, &qdrops_long);
282 qdrops = qdrops_long;
283 error = sysctl_handle_int(oidp, &qdrops, 0, req);
284 if (error || !req->newptr)
288 netisr_clearqdrops(&ip_nh);
292 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops,
293 CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_queue_drops, "I",
294 "Number of packets dropped from the IP input queue");
298 sysctl_netinet_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
302 netisr_getqlimit(&ip_direct_nh, &qlimit);
303 error = sysctl_handle_int(oidp, &qlimit, 0, req);
304 if (error || !req->newptr)
308 return (netisr_setqlimit(&ip_direct_nh, qlimit));
310 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_direct_queue_maxlen,
311 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_direct_queue_maxlen, "I",
312 "Maximum size of the IP direct input queue");
315 sysctl_netinet_intr_direct_queue_drops(SYSCTL_HANDLER_ARGS)
317 u_int64_t qdrops_long;
320 netisr_getqdrops(&ip_direct_nh, &qdrops_long);
321 qdrops = qdrops_long;
322 error = sysctl_handle_int(oidp, &qdrops, 0, req);
323 if (error || !req->newptr)
327 netisr_clearqdrops(&ip_direct_nh);
331 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_direct_queue_drops,
332 CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_direct_queue_drops, "I",
333 "Number of packets dropped from the IP direct input queue");
337 * IP initialization: fill in IP protocol switch table.
338 * All protocols not implemented in kernel go to raw IP protocol handler.
346 TAILQ_INIT(&V_in_ifaddrhead);
347 V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
349 /* Initialize IP reassembly queue. */
350 for (i = 0; i < IPREASS_NHASH; i++)
351 TAILQ_INIT(&V_ipq[i]);
352 V_maxnipq = nmbclusters / 32;
353 V_maxfragsperpacket = 16;
354 V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
355 NULL, UMA_ALIGN_PTR, 0);
358 /* Initialize packet filter hooks. */
359 V_inet_pfil_hook.ph_type = PFIL_TYPE_AF;
360 V_inet_pfil_hook.ph_af = AF_INET;
361 if ((i = pfil_head_register(&V_inet_pfil_hook)) != 0)
362 printf("%s: WARNING: unable to register pfil hook, "
363 "error %d\n", __func__, i);
365 /* Skip initialization of globals for non-default instances. */
366 if (!IS_DEFAULT_VNET(curvnet))
369 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
371 panic("ip_init: PF_INET not found");
373 /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
374 for (i = 0; i < IPPROTO_MAX; i++)
375 ip_protox[i] = pr - inetsw;
377 * Cycle through IP protocols and put them into the appropriate place
380 for (pr = inetdomain.dom_protosw;
381 pr < inetdomain.dom_protoswNPROTOSW; pr++)
382 if (pr->pr_domain->dom_family == PF_INET &&
383 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
384 /* Be careful to only index valid IP protocols. */
385 if (pr->pr_protocol < IPPROTO_MAX)
386 ip_protox[pr->pr_protocol] = pr - inetsw;
389 EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
390 NULL, EVENTHANDLER_PRI_ANY);
392 /* Initialize various other remaining things. */
393 for (i = 0; i < IPREASS_NHASH; i++)
395 netisr_register(&ip_nh);
397 netisr_register(&ip_direct_nh);
407 if ((i = pfil_head_unregister(&V_inet_pfil_hook)) != 0)
408 printf("%s: WARNING: unable to unregister pfil hook, "
409 "error %d\n", __func__, i);
411 /* Cleanup in_ifaddr hash table; should be empty. */
412 hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask);
416 uma_zdestroy(V_ipq_zone);
422 * IP direct input routine.
424 * This is called when reinjecting completed fragments where
425 * all of the previous checking and book-keeping has been done.
428 ip_direct_input(struct mbuf *m)
433 ip = mtod(m, struct ip *);
434 hlen = ip->ip_hl << 2;
436 IPSTAT_INC(ips_delivered);
437 (*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p);
443 * Ip input routine. Checksum and byte swap header. If fragmented
444 * try to reassemble. Process options. Pass to next level.
447 ip_input(struct mbuf *m)
449 struct ip *ip = NULL;
450 struct in_ifaddr *ia = NULL;
453 int checkif, hlen = 0;
454 uint16_t sum, ip_len;
455 int dchg = 0; /* dest changed after fw */
456 struct in_addr odst; /* original dst address */
460 if (m->m_flags & M_FASTFWD_OURS) {
461 m->m_flags &= ~M_FASTFWD_OURS;
462 /* Set up some basics that will be used later. */
463 ip = mtod(m, struct ip *);
464 hlen = ip->ip_hl << 2;
465 ip_len = ntohs(ip->ip_len);
469 IPSTAT_INC(ips_total);
471 if (m->m_pkthdr.len < sizeof(struct ip))
474 if (m->m_len < sizeof (struct ip) &&
475 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
476 IPSTAT_INC(ips_toosmall);
479 ip = mtod(m, struct ip *);
481 if (ip->ip_v != IPVERSION) {
482 IPSTAT_INC(ips_badvers);
486 hlen = ip->ip_hl << 2;
487 if (hlen < sizeof(struct ip)) { /* minimum header length */
488 IPSTAT_INC(ips_badhlen);
491 if (hlen > m->m_len) {
492 if ((m = m_pullup(m, hlen)) == NULL) {
493 IPSTAT_INC(ips_badhlen);
496 ip = mtod(m, struct ip *);
499 IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL);
501 /* 127/8 must not appear on wire - RFC1122 */
502 ifp = m->m_pkthdr.rcvif;
503 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
504 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
505 if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
506 IPSTAT_INC(ips_badaddr);
511 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
512 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
514 if (hlen == sizeof(struct ip)) {
515 sum = in_cksum_hdr(ip);
517 sum = in_cksum(m, hlen);
521 IPSTAT_INC(ips_badsum);
526 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
527 /* packet is dropped by traffic conditioner */
531 ip_len = ntohs(ip->ip_len);
533 IPSTAT_INC(ips_badlen);
538 * Check that the amount of data in the buffers
539 * is as at least much as the IP header would have us expect.
540 * Trim mbufs if longer than we expect.
541 * Drop packet if shorter than we expect.
543 if (m->m_pkthdr.len < ip_len) {
545 IPSTAT_INC(ips_tooshort);
548 if (m->m_pkthdr.len > ip_len) {
549 if (m->m_len == m->m_pkthdr.len) {
551 m->m_pkthdr.len = ip_len;
553 m_adj(m, ip_len - m->m_pkthdr.len);
558 * Bypass packet filtering for packets previously handled by IPsec.
560 if (ip_ipsec_filtertunnel(m))
565 * Run through list of hooks for input packets.
567 * NB: Beware of the destination address changing (e.g.
568 * by NAT rewriting). When this happens, tell
569 * ip_forward to do the right thing.
572 /* Jump over all PFIL processing if hooks are not active. */
573 if (!PFIL_HOOKED(&V_inet_pfil_hook))
577 if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0)
579 if (m == NULL) /* consumed by filter */
582 ip = mtod(m, struct ip *);
583 dchg = (odst.s_addr != ip->ip_dst.s_addr);
584 ifp = m->m_pkthdr.rcvif;
586 if (m->m_flags & M_FASTFWD_OURS) {
587 m->m_flags &= ~M_FASTFWD_OURS;
590 if (m->m_flags & M_IP_NEXTHOP) {
591 dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL);
594 * Directly ship the packet on. This allows
595 * forwarding packets originally destined to us
596 * to some other directly connected host.
605 * Process options and, if not destined for us,
606 * ship it on. ip_dooptions returns 1 when an
607 * error was detected (causing an icmp message
608 * to be sent and the original packet to be freed).
610 if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
613 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
614 * matter if it is destined to another node, or whether it is
615 * a multicast one, RSVP wants it! and prevents it from being forwarded
616 * anywhere else. Also checks if the rsvp daemon is running before
617 * grabbing the packet.
619 if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
623 * Check our list of addresses, to see if the packet is for us.
624 * If we don't have any addresses, assume any unicast packet
625 * we receive might be for us (and let the upper layers deal
628 if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
629 (m->m_flags & (M_MCAST|M_BCAST)) == 0)
633 * Enable a consistency check between the destination address
634 * and the arrival interface for a unicast packet (the RFC 1122
635 * strong ES model) if IP forwarding is disabled and the packet
636 * is not locally generated and the packet is not subject to
639 * XXX - Checking also should be disabled if the destination
640 * address is ipnat'ed to a different interface.
642 * XXX - Checking is incompatible with IP aliases added
643 * to the loopback interface instead of the interface where
644 * the packets are received.
646 * XXX - This is the case for carp vhost IPs as well so we
647 * insert a workaround. If the packet got here, we already
648 * checked with carp_iamatch() and carp_forus().
650 checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
651 ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
652 ifp->if_carp == NULL && (dchg == 0);
655 * Check for exact addresses in the hash bucket.
657 /* IN_IFADDR_RLOCK(); */
658 LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
660 * If the address matches, verify that the packet
661 * arrived via the correct interface if checking is
664 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
665 (!checkif || ia->ia_ifp == ifp)) {
666 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
667 counter_u64_add(ia->ia_ifa.ifa_ibytes,
669 /* IN_IFADDR_RUNLOCK(); */
673 /* IN_IFADDR_RUNLOCK(); */
676 * Check for broadcast addresses.
678 * Only accept broadcast packets that arrive via the matching
679 * interface. Reception of forwarded directed broadcasts would
680 * be handled via ip_forward() and ether_output() with the loopback
681 * into the stack for SIMPLEX interfaces handled by ether_output().
683 if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
685 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
686 if (ifa->ifa_addr->sa_family != AF_INET)
689 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
691 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
692 counter_u64_add(ia->ia_ifa.ifa_ibytes,
694 IF_ADDR_RUNLOCK(ifp);
698 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
699 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
700 counter_u64_add(ia->ia_ifa.ifa_ibytes,
702 IF_ADDR_RUNLOCK(ifp);
707 IF_ADDR_RUNLOCK(ifp);
710 /* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
711 if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
712 IPSTAT_INC(ips_cantforward);
716 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
719 * If we are acting as a multicast router, all
720 * incoming multicast packets are passed to the
721 * kernel-level multicast forwarding function.
722 * The packet is returned (relatively) intact; if
723 * ip_mforward() returns a non-zero value, the packet
724 * must be discarded, else it may be accepted below.
726 if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
727 IPSTAT_INC(ips_cantforward);
733 * The process-level routing daemon needs to receive
734 * all multicast IGMP packets, whether or not this
735 * host belongs to their destination groups.
737 if (ip->ip_p == IPPROTO_IGMP)
739 IPSTAT_INC(ips_forward);
742 * Assume the packet is for us, to avoid prematurely taking
743 * a lock on the in_multi hash. Protocols must perform
744 * their own filtering and update statistics accordingly.
748 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
750 if (ip->ip_dst.s_addr == INADDR_ANY)
754 * Not for us; forward if possible and desirable.
756 if (V_ipforwarding == 0) {
757 IPSTAT_INC(ips_cantforward);
767 * IPSTEALTH: Process non-routing options only
768 * if the packet is destined for us.
770 if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1))
772 #endif /* IPSTEALTH */
775 * Attempt reassembly; if it succeeds, proceed.
776 * ip_reass() will return a different mbuf.
778 if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
779 /* XXXGL: shouldn't we save & set m_flags? */
783 ip = mtod(m, struct ip *);
784 /* Get the header length of the reassembled packet */
785 hlen = ip->ip_hl << 2;
790 * enforce IPsec policy checking if we are seeing last header.
791 * note that we do not visit this with protocols with pcb layer
792 * code - like udp/tcp/raw ip.
794 if (ip_ipsec_input(m, ip->ip_p) != 0)
799 * Switch out to protocol's input routine.
801 IPSTAT_INC(ips_delivered);
803 (*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p);
810 * After maxnipq has been updated, propagate the change to UMA. The UMA zone
811 * max has slightly different semantics than the sysctl, for historical
819 * -1 for unlimited allocation.
822 uma_zone_set_max(V_ipq_zone, 0);
824 * Positive number for specific bound.
827 uma_zone_set_max(V_ipq_zone, V_maxnipq);
829 * Zero specifies no further fragment queue allocation.
831 if (V_maxnipq == 0) {
832 uma_zone_set_max(V_ipq_zone, 1);
838 ipq_zone_change(void *tag)
841 if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
842 V_maxnipq = nmbclusters / 32;
848 sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
853 error = sysctl_handle_int(oidp, &i, 0, req);
854 if (error || !req->newptr)
858 * XXXRW: Might be a good idea to sanity check the argument and place
859 * an extreme upper bound.
868 SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
869 NULL, 0, sysctl_maxnipq, "I",
870 "Maximum number of IPv4 fragment reassembly queue entries");
872 #define M_IP_FRAG M_PROTO9
875 * Attempt to purge something from the reassembly queue to make
878 * Must be called without any IPQ locks held, as it will attempt
879 * to lock each in turn.
881 * 'skip_bucket' is the bucket with which to skip over, or -1 to
882 * not skip over anything.
884 * Returns the bucket being freed, or -1 for no action.
887 ip_reass_purge_element(int skip_bucket)
892 for (i = 0; i < IPREASS_NHASH; i++) {
893 if (skip_bucket > -1 && i == skip_bucket)
896 r = TAILQ_LAST(&V_ipq[i], ipqhead);
898 ipq_timeout(&V_ipq[i], r);
908 * Take incoming datagram fragment and try to reassemble it into
909 * whole datagram. If the argument is the first fragment or one
910 * in between the function will return NULL and store the mbuf
911 * in the fragment chain. If the argument is the last fragment
912 * the packet will be reassembled and the pointer to the new
913 * mbuf returned for further processing. Only m_tags attached
914 * to the first packet/fragment are preserved.
915 * The IP header is *NOT* adjusted out of iplen.
918 ip_reass(struct mbuf *m)
921 struct mbuf *p, *q, *nq, *t;
922 struct ipq *fp = NULL;
923 struct ipqhead *head;
928 uint32_t rss_hash, rss_type;
932 /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
933 if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
934 IPSTAT_INC(ips_fragments);
935 IPSTAT_INC(ips_fragdropped);
940 ip = mtod(m, struct ip *);
941 hlen = ip->ip_hl << 2;
943 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
948 * Look for queue of fragments
951 TAILQ_FOREACH(fp, head, ipq_list)
952 if (ip->ip_id == fp->ipq_id &&
953 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
954 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
956 mac_ipq_match(m, fp) &&
958 ip->ip_p == fp->ipq_p)
964 * Attempt to trim the number of allocated fragment queues if it
965 * exceeds the administrative limit.
967 if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
969 * drop something from the tail of the current queue
970 * before proceeding further
972 struct ipq *q = TAILQ_LAST(head, ipqhead);
973 if (q == NULL) { /* gak */
975 * Defer doing this until later; when the
976 * lock is no longer held.
980 ipq_timeout(head, q);
985 * Adjust ip_len to not reflect header,
986 * convert offset of this to bytes.
988 ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
989 if (ip->ip_off & htons(IP_MF)) {
991 * Make sure that fragments have a data length
992 * that's a non-zero multiple of 8 bytes.
994 if (ip->ip_len == htons(0) || (ntohs(ip->ip_len) & 0x7) != 0) {
995 IPSTAT_INC(ips_toosmall); /* XXX */
998 m->m_flags |= M_IP_FRAG;
1000 m->m_flags &= ~M_IP_FRAG;
1001 ip->ip_off = htons(ntohs(ip->ip_off) << 3);
1004 * Attempt reassembly; if it succeeds, proceed.
1005 * ip_reass() will return a different mbuf.
1007 IPSTAT_INC(ips_fragments);
1008 m->m_pkthdr.PH_loc.ptr = ip;
1010 /* Previous ip_reass() started here. */
1012 * Presence of header sizes in mbufs
1013 * would confuse code below.
1019 * If first fragment to arrive, create a reassembly queue.
1022 fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
1026 if (mac_ipq_init(fp, M_NOWAIT) != 0) {
1027 uma_zfree(V_ipq_zone, fp);
1031 mac_ipq_create(m, fp);
1033 TAILQ_INSERT_HEAD(head, fp, ipq_list);
1036 fp->ipq_ttl = IPFRAGTTL;
1037 fp->ipq_p = ip->ip_p;
1038 fp->ipq_id = ip->ip_id;
1039 fp->ipq_src = ip->ip_src;
1040 fp->ipq_dst = ip->ip_dst;
1042 m->m_nextpkt = NULL;
1047 mac_ipq_update(m, fp);
1051 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.PH_loc.ptr))
1054 * Handle ECN by comparing this segment with the first one;
1055 * if CE is set, do not lose CE.
1056 * drop if CE and not-ECT are mixed for the same packet.
1058 ecn = ip->ip_tos & IPTOS_ECN_MASK;
1059 ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
1060 if (ecn == IPTOS_ECN_CE) {
1061 if (ecn0 == IPTOS_ECN_NOTECT)
1063 if (ecn0 != IPTOS_ECN_CE)
1064 GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
1066 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
1070 * Find a segment which begins after this one does.
1072 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
1073 if (ntohs(GETIP(q)->ip_off) > ntohs(ip->ip_off))
1077 * If there is a preceding segment, it may provide some of
1078 * our data already. If so, drop the data from the incoming
1079 * segment. If it provides all of our data, drop us, otherwise
1080 * stick new segment in the proper place.
1082 * If some of the data is dropped from the preceding
1083 * segment, then it's checksum is invalidated.
1086 i = ntohs(GETIP(p)->ip_off) + ntohs(GETIP(p)->ip_len) -
1089 if (i >= ntohs(ip->ip_len))
1092 m->m_pkthdr.csum_flags = 0;
1093 ip->ip_off = htons(ntohs(ip->ip_off) + i);
1094 ip->ip_len = htons(ntohs(ip->ip_len) - i);
1096 m->m_nextpkt = p->m_nextpkt;
1099 m->m_nextpkt = fp->ipq_frags;
1104 * While we overlap succeeding segments trim them or,
1105 * if they are completely covered, dequeue them.
1107 for (; q != NULL && ntohs(ip->ip_off) + ntohs(ip->ip_len) >
1108 ntohs(GETIP(q)->ip_off); q = nq) {
1109 i = (ntohs(ip->ip_off) + ntohs(ip->ip_len)) -
1110 ntohs(GETIP(q)->ip_off);
1111 if (i < ntohs(GETIP(q)->ip_len)) {
1112 GETIP(q)->ip_len = htons(ntohs(GETIP(q)->ip_len) - i);
1113 GETIP(q)->ip_off = htons(ntohs(GETIP(q)->ip_off) + i);
1115 q->m_pkthdr.csum_flags = 0;
1120 IPSTAT_INC(ips_fragdropped);
1126 * Check for complete reassembly and perform frag per packet
1129 * Frag limiting is performed here so that the nth frag has
1130 * a chance to complete the packet before we drop the packet.
1131 * As a result, n+1 frags are actually allowed per packet, but
1132 * only n will ever be stored. (n = maxfragsperpacket.)
1136 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1137 if (ntohs(GETIP(q)->ip_off) != next) {
1138 if (fp->ipq_nfrags > V_maxfragsperpacket)
1142 next += ntohs(GETIP(q)->ip_len);
1144 /* Make sure the last packet didn't have the IP_MF flag */
1145 if (p->m_flags & M_IP_FRAG) {
1146 if (fp->ipq_nfrags > V_maxfragsperpacket)
1152 * Reassembly is complete. Make sure the packet is a sane size.
1156 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
1157 IPSTAT_INC(ips_toolong);
1163 * Concatenate fragments.
1170 q->m_nextpkt = NULL;
1171 for (q = nq; q != NULL; q = nq) {
1173 q->m_nextpkt = NULL;
1174 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1175 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1179 * In order to do checksumming faster we do 'end-around carry' here
1180 * (and not in for{} loop), though it implies we are not going to
1181 * reassemble more than 64k fragments.
1183 while (m->m_pkthdr.csum_data & 0xffff0000)
1184 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
1185 (m->m_pkthdr.csum_data >> 16);
1187 mac_ipq_reassemble(fp, m);
1188 mac_ipq_destroy(fp);
1192 * Create header for new ip packet by modifying header of first
1193 * packet; dequeue and discard fragment reassembly header.
1194 * Make header visible.
1196 ip->ip_len = htons((ip->ip_hl << 2) + next);
1197 ip->ip_src = fp->ipq_src;
1198 ip->ip_dst = fp->ipq_dst;
1199 TAILQ_REMOVE(head, fp, ipq_list);
1201 uma_zfree(V_ipq_zone, fp);
1202 m->m_len += (ip->ip_hl << 2);
1203 m->m_data -= (ip->ip_hl << 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 */
1207 IPSTAT_INC(ips_reassembled);
1211 * Do the delayed purge to keep fragment counts under
1212 * the configured maximum.
1214 * This is delayed so that it's not done with another IPQ bucket
1217 * Note that we pass in the bucket to /skip/ over, not
1218 * the bucket to /purge/.
1221 ip_reass_purge_element(hash);
1225 * Query the RSS layer for the flowid / flowtype for the
1228 * For now, just assume we have to calculate a new one.
1229 * Later on we should check to see if the assigned flowid matches
1230 * what RSS wants for the given IP protocol and if so, just keep it.
1232 * We then queue into the relevant netisr so it can be dispatched
1233 * to the correct CPU.
1235 * Note - this may return 1, which means the flowid in the mbuf
1236 * is correct for the configured RSS hash types and can be used.
1238 if (rss_mbuf_software_hash_v4(m, 0, &rss_hash, &rss_type) == 0) {
1239 m->m_pkthdr.flowid = rss_hash;
1240 M_HASHTYPE_SET(m, rss_type);
1244 * Queue/dispatch for reprocessing.
1246 * Note: this is much slower than just handling the frame in the
1247 * current receive context. It's likely worth investigating
1250 netisr_dispatch(NETISR_IP_DIRECT, m);
1254 /* Handle in-line */
1258 IPSTAT_INC(ips_fragdropped);
1270 * Free a fragment reassembly header and all
1271 * associated datagrams.
1274 ipq_free(struct ipqhead *fhp, struct ipq *fp)
1278 while (fp->ipq_frags) {
1280 fp->ipq_frags = q->m_nextpkt;
1283 TAILQ_REMOVE(fhp, fp, ipq_list);
1284 uma_zfree(V_ipq_zone, fp);
1289 * IP timer processing;
1290 * if a timer expires on a reassembly
1291 * queue, discard it.
1296 VNET_ITERATOR_DECL(vnet_iter);
1297 struct ipq *fp, *tmp;
1300 VNET_LIST_RLOCK_NOSLEEP();
1301 VNET_FOREACH(vnet_iter) {
1302 CURVNET_SET(vnet_iter);
1303 for (i = 0; i < IPREASS_NHASH; i++) {
1305 TAILQ_FOREACH_SAFE(fp, &V_ipq[i], ipq_list, tmp)
1306 if (--fp->ipq_ttl == 0)
1307 ipq_timeout(&V_ipq[i], fp);
1311 * If we are over the maximum number of fragments
1312 * (due to the limit being lowered), drain off
1313 * enough to get down to the new limit.
1315 if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1316 for (i = 0; i < IPREASS_NHASH; i++) {
1318 while (V_nipq > V_maxnipq &&
1319 !TAILQ_EMPTY(&V_ipq[i]))
1321 TAILQ_FIRST(&V_ipq[i]));
1327 VNET_LIST_RUNLOCK_NOSLEEP();
1331 * Drain off all datagram fragments.
1338 for (i = 0; i < IPREASS_NHASH; i++) {
1340 while(!TAILQ_EMPTY(&V_ipq[i]))
1341 ipq_drop(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1349 VNET_ITERATOR_DECL(vnet_iter);
1351 VNET_LIST_RLOCK_NOSLEEP();
1352 VNET_FOREACH(vnet_iter) {
1353 CURVNET_SET(vnet_iter);
1357 VNET_LIST_RUNLOCK_NOSLEEP();
1361 * The protocol to be inserted into ip_protox[] must be already registered
1362 * in inetsw[], either statically or through pf_proto_register().
1365 ipproto_register(short ipproto)
1369 /* Sanity checks. */
1370 if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1371 return (EPROTONOSUPPORT);
1374 * The protocol slot must not be occupied by another protocol
1375 * already. An index pointing to IPPROTO_RAW is unused.
1377 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1379 return (EPFNOSUPPORT);
1380 if (ip_protox[ipproto] != pr - inetsw) /* IPPROTO_RAW */
1383 /* Find the protocol position in inetsw[] and set the index. */
1384 for (pr = inetdomain.dom_protosw;
1385 pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1386 if (pr->pr_domain->dom_family == PF_INET &&
1387 pr->pr_protocol && pr->pr_protocol == ipproto) {
1388 ip_protox[pr->pr_protocol] = pr - inetsw;
1392 return (EPROTONOSUPPORT);
1396 ipproto_unregister(short ipproto)
1400 /* Sanity checks. */
1401 if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1402 return (EPROTONOSUPPORT);
1404 /* Check if the protocol was indeed registered. */
1405 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1407 return (EPFNOSUPPORT);
1408 if (ip_protox[ipproto] == pr - inetsw) /* IPPROTO_RAW */
1411 /* Reset the protocol slot to IPPROTO_RAW. */
1412 ip_protox[ipproto] = pr - inetsw;
1417 * Given address of next destination (final or next hop), return (referenced)
1418 * internet address info of interface to be used to get there.
1421 ip_rtaddr(struct in_addr dst, u_int fibnum)
1424 struct sockaddr_in *sin;
1425 struct in_ifaddr *ia;
1427 bzero(&sro, sizeof(sro));
1428 sin = (struct sockaddr_in *)&sro.ro_dst;
1429 sin->sin_family = AF_INET;
1430 sin->sin_len = sizeof(*sin);
1431 sin->sin_addr = dst;
1432 in_rtalloc_ign(&sro, 0, fibnum);
1434 if (sro.ro_rt == NULL)
1437 ia = ifatoia(sro.ro_rt->rt_ifa);
1438 ifa_ref(&ia->ia_ifa);
1443 u_char inetctlerrmap[PRC_NCMDS] = {
1445 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1446 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1447 EMSGSIZE, EHOSTUNREACH, 0, 0,
1448 0, 0, EHOSTUNREACH, 0,
1449 ENOPROTOOPT, ECONNREFUSED
1453 * Forward a packet. If some error occurs return the sender
1454 * an icmp packet. Note we can't always generate a meaningful
1455 * icmp message because icmp doesn't have a large enough repertoire
1456 * of codes and types.
1458 * If not forwarding, just drop the packet. This could be confusing
1459 * if ipforwarding was zero but some routing protocol was advancing
1460 * us as a gateway to somewhere. However, we must let the routing
1461 * protocol deal with that.
1463 * The srcrt parameter indicates whether the packet is being forwarded
1464 * via a source route.
1467 ip_forward(struct mbuf *m, int srcrt)
1469 struct ip *ip = mtod(m, struct ip *);
1470 struct in_ifaddr *ia;
1472 struct in_addr dest;
1474 int error, type = 0, code = 0, mtu = 0;
1476 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1477 IPSTAT_INC(ips_cantforward);
1482 if (ip_ipsec_fwd(m) != 0) {
1483 IPSTAT_INC(ips_cantforward);
1491 if (ip->ip_ttl <= IPTTLDEC) {
1492 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1500 ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1503 * 'ia' may be NULL if there is no route for this destination.
1504 * In case of IPsec, Don't discard it just yet, but pass it to
1505 * ip_output in case of outgoing IPsec policy.
1507 if (!srcrt && ia == NULL) {
1508 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1514 * Save the IP header and at most 8 bytes of the payload,
1515 * in case we need to generate an ICMP message to the src.
1517 * XXX this can be optimized a lot by saving the data in a local
1518 * buffer on the stack (72 bytes at most), and only allocating the
1519 * mbuf if really necessary. The vast majority of the packets
1520 * are forwarded without having to send an ICMP back (either
1521 * because unnecessary, or because rate limited), so we are
1522 * really we are wasting a lot of work here.
1524 * We don't use m_copy() because it might return a reference
1525 * to a shared cluster. Both this function and ip_output()
1526 * assume exclusive access to the IP header in `m', so any
1527 * data in a cluster may change before we reach icmp_error().
1529 mcopy = m_gethdr(M_NOWAIT, m->m_type);
1530 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
1532 * It's probably ok if the pkthdr dup fails (because
1533 * the deep copy of the tag chain failed), but for now
1534 * be conservative and just discard the copy since
1535 * code below may some day want the tags.
1540 if (mcopy != NULL) {
1541 mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy));
1542 mcopy->m_pkthdr.len = mcopy->m_len;
1543 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1549 ip->ip_ttl -= IPTTLDEC;
1555 * If forwarding packet using same interface that it came in on,
1556 * perhaps should send a redirect to sender to shortcut a hop.
1557 * Only send redirect if source is sending directly to us,
1558 * and if packet was not source routed (or has any options).
1559 * Also, don't send redirect if forwarding using a default route
1560 * or a route modified by a redirect.
1563 if (!srcrt && V_ipsendredirects &&
1564 ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
1565 struct sockaddr_in *sin;
1568 bzero(&ro, sizeof(ro));
1569 sin = (struct sockaddr_in *)&ro.ro_dst;
1570 sin->sin_family = AF_INET;
1571 sin->sin_len = sizeof(*sin);
1572 sin->sin_addr = ip->ip_dst;
1573 in_rtalloc_ign(&ro, 0, M_GETFIB(m));
1577 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1578 satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1579 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1580 u_long src = ntohl(ip->ip_src.s_addr);
1583 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1584 if (rt->rt_flags & RTF_GATEWAY)
1585 dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1587 dest.s_addr = ip->ip_dst.s_addr;
1588 /* Router requirements says to only send host redirects */
1589 type = ICMP_REDIRECT;
1590 code = ICMP_REDIRECT_HOST;
1598 * Try to cache the route MTU from ip_output so we can consider it for
1599 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1601 bzero(&ro, sizeof(ro));
1603 error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1605 if (error == EMSGSIZE && ro.ro_rt)
1606 mtu = ro.ro_rt->rt_mtu;
1610 IPSTAT_INC(ips_cantforward);
1612 IPSTAT_INC(ips_forward);
1614 IPSTAT_INC(ips_redirectsent);
1619 ifa_free(&ia->ia_ifa);
1623 if (mcopy == NULL) {
1625 ifa_free(&ia->ia_ifa);
1631 case 0: /* forwarded, but need redirect */
1632 /* type, code set above */
1640 type = ICMP_UNREACH;
1641 code = ICMP_UNREACH_HOST;
1645 type = ICMP_UNREACH;
1646 code = ICMP_UNREACH_NEEDFRAG;
1650 * If IPsec is configured for this path,
1651 * override any possibly mtu value set by ip_output.
1653 mtu = ip_ipsec_mtu(mcopy, mtu);
1656 * If the MTU was set before make sure we are below the
1658 * If the MTU wasn't set before use the interface mtu or
1659 * fall back to the next smaller mtu step compared to the
1660 * current packet size.
1664 mtu = min(mtu, ia->ia_ifp->if_mtu);
1667 mtu = ia->ia_ifp->if_mtu;
1669 mtu = ip_next_mtu(ntohs(ip->ip_len), 0);
1671 IPSTAT_INC(ips_cantfrag);
1675 case EACCES: /* ipfw denied packet */
1678 ifa_free(&ia->ia_ifa);
1682 ifa_free(&ia->ia_ifa);
1683 icmp_error(mcopy, type, code, dest.s_addr, mtu);
1687 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1691 if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1695 if (inp->inp_socket->so_options & SO_BINTIME) {
1696 *mp = sbcreatecontrol((caddr_t)&bt, sizeof(bt),
1697 SCM_BINTIME, SOL_SOCKET);
1699 mp = &(*mp)->m_next;
1701 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1704 bintime2timeval(&bt, &tv);
1705 *mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv),
1706 SCM_TIMESTAMP, SOL_SOCKET);
1708 mp = &(*mp)->m_next;
1711 if (inp->inp_flags & INP_RECVDSTADDR) {
1712 *mp = sbcreatecontrol((caddr_t)&ip->ip_dst,
1713 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1715 mp = &(*mp)->m_next;
1717 if (inp->inp_flags & INP_RECVTTL) {
1718 *mp = sbcreatecontrol((caddr_t)&ip->ip_ttl,
1719 sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
1721 mp = &(*mp)->m_next;
1725 * Moving these out of udp_input() made them even more broken
1726 * than they already were.
1728 /* options were tossed already */
1729 if (inp->inp_flags & INP_RECVOPTS) {
1730 *mp = sbcreatecontrol((caddr_t)opts_deleted_above,
1731 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1733 mp = &(*mp)->m_next;
1735 /* ip_srcroute doesn't do what we want here, need to fix */
1736 if (inp->inp_flags & INP_RECVRETOPTS) {
1737 *mp = sbcreatecontrol((caddr_t)ip_srcroute(m),
1738 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1740 mp = &(*mp)->m_next;
1743 if (inp->inp_flags & INP_RECVIF) {
1746 struct sockaddr_dl sdl;
1749 struct sockaddr_dl *sdp;
1750 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1752 if ((ifp = m->m_pkthdr.rcvif) &&
1753 ifp->if_index && ifp->if_index <= V_if_index) {
1754 sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1756 * Change our mind and don't try copy.
1758 if (sdp->sdl_family != AF_LINK ||
1759 sdp->sdl_len > sizeof(sdlbuf)) {
1762 bcopy(sdp, sdl2, sdp->sdl_len);
1766 offsetof(struct sockaddr_dl, sdl_data[0]);
1767 sdl2->sdl_family = AF_LINK;
1768 sdl2->sdl_index = 0;
1769 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1771 *mp = sbcreatecontrol((caddr_t)sdl2, sdl2->sdl_len,
1772 IP_RECVIF, IPPROTO_IP);
1774 mp = &(*mp)->m_next;
1776 if (inp->inp_flags & INP_RECVTOS) {
1777 *mp = sbcreatecontrol((caddr_t)&ip->ip_tos,
1778 sizeof(u_char), IP_RECVTOS, IPPROTO_IP);
1780 mp = &(*mp)->m_next;
1783 if (inp->inp_flags2 & INP_RECVFLOWID) {
1784 uint32_t flowid, flow_type;
1786 flowid = m->m_pkthdr.flowid;
1787 flow_type = M_HASHTYPE_GET(m);
1790 * XXX should handle the failure of one or the
1791 * other - don't populate both?
1793 *mp = sbcreatecontrol((caddr_t) &flowid,
1794 sizeof(uint32_t), IP_FLOWID, IPPROTO_IP);
1796 mp = &(*mp)->m_next;
1797 *mp = sbcreatecontrol((caddr_t) &flow_type,
1798 sizeof(uint32_t), IP_FLOWTYPE, IPPROTO_IP);
1800 mp = &(*mp)->m_next;
1804 if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
1805 uint32_t flowid, flow_type;
1806 uint32_t rss_bucketid;
1808 flowid = m->m_pkthdr.flowid;
1809 flow_type = M_HASHTYPE_GET(m);
1811 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1812 *mp = sbcreatecontrol((caddr_t) &rss_bucketid,
1813 sizeof(uint32_t), IP_RSSBUCKETID, IPPROTO_IP);
1815 mp = &(*mp)->m_next;
1822 * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
1823 * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
1824 * locking. This code remains in ip_input.c as ip_mroute.c is optionally
1827 static VNET_DEFINE(int, ip_rsvp_on);
1828 VNET_DEFINE(struct socket *, ip_rsvpd);
1830 #define V_ip_rsvp_on VNET(ip_rsvp_on)
1833 ip_rsvp_init(struct socket *so)
1836 if (so->so_type != SOCK_RAW ||
1837 so->so_proto->pr_protocol != IPPROTO_RSVP)
1840 if (V_ip_rsvpd != NULL)
1845 * This may seem silly, but we need to be sure we don't over-increment
1846 * the RSVP counter, in case something slips up.
1848 if (!V_ip_rsvp_on) {
1862 * This may seem silly, but we need to be sure we don't over-decrement
1863 * the RSVP counter, in case something slips up.
1873 rsvp_input(struct mbuf **mp, int *offp, int proto)
1880 if (rsvp_input_p) { /* call the real one if loaded */
1882 rsvp_input_p(mp, offp, proto);
1883 return (IPPROTO_DONE);
1886 /* Can still get packets with rsvp_on = 0 if there is a local member
1887 * of the group to which the RSVP packet is addressed. But in this
1888 * case we want to throw the packet away.
1893 return (IPPROTO_DONE);
1896 if (V_ip_rsvpd != NULL) {
1898 rip_input(mp, offp, proto);
1899 return (IPPROTO_DONE);
1901 /* Drop the packet */
1903 return (IPPROTO_DONE);