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"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
52 #include <sys/kernel.h>
54 #include <sys/rwlock.h>
55 #include <sys/syslog.h>
56 #include <sys/sysctl.h>
57 #include <sys/vimage.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>
67 #include <net/flowtable.h>
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/in_var.h>
72 #include <netinet/ip.h>
73 #include <netinet/in_pcb.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/ip_icmp.h>
76 #include <netinet/ip_options.h>
77 #include <machine/in_cksum.h>
78 #include <netinet/vinet.h>
80 #include <netinet/ip_carp.h>
83 #include <netinet/ip_ipsec.h>
86 #include <sys/socketvar.h>
88 /* XXX: Temporary until ipfw_ether and ipfw_bridge are converted. */
89 #include <netinet/ip_fw.h>
90 #include <netinet/ip_dummynet.h>
92 #include <security/mac/mac_framework.h>
95 CTASSERT(sizeof(struct ip) == 20);
99 #ifndef VIMAGE_GLOBALS
100 struct vnet_inet vnet_inet_0;
104 #ifdef VIMAGE_GLOBALS
105 static int ipsendredirects;
106 static int ip_checkinterface;
107 static int ip_keepfaith;
108 static int ip_sendsourcequench;
112 struct in_ifaddrhead in_ifaddrhead; /* first inet address */
113 struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */
114 u_long in_ifaddrhmask; /* mask for hash table */
115 struct ipstat ipstat;
116 static int ip_rsvp_on;
117 struct socket *ip_rsvpd;
119 static struct ipqhead ipq[IPREASS_NHASH];
120 static int maxnipq; /* Administrative limit on # reass queues. */
121 static int maxfragsperpacket;
123 static int nipq; /* Total # of reass queues */
126 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_FORWARDING,
127 forwarding, CTLFLAG_RW, ipforwarding, 0,
128 "Enable IP forwarding between interfaces");
130 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_SENDREDIRECTS,
131 redirect, CTLFLAG_RW, ipsendredirects, 0,
132 "Enable sending IP redirects");
134 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_DEFTTL,
135 ttl, CTLFLAG_RW, ip_defttl, 0, "Maximum TTL on IP packets");
137 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_KEEPFAITH,
138 keepfaith, CTLFLAG_RW, ip_keepfaith, 0,
139 "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
141 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO,
142 sendsourcequench, CTLFLAG_RW, ip_sendsourcequench, 0,
143 "Enable the transmission of source quench packets");
145 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, random_id,
146 CTLFLAG_RW, ip_do_randomid, 0, "Assign random ip_id values");
149 * XXX - Setting ip_checkinterface mostly implements the receive side of
150 * the Strong ES model described in RFC 1122, but since the routing table
151 * and transmit implementation do not implement the Strong ES model,
152 * setting this to 1 results in an odd hybrid.
154 * XXX - ip_checkinterface currently must be disabled if you use ipnat
155 * to translate the destination address to another local interface.
157 * XXX - ip_checkinterface must be disabled if you add IP aliases
158 * to the loopback interface instead of the interface where the
159 * packets for those addresses are received.
161 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO,
162 check_interface, CTLFLAG_RW, ip_checkinterface, 0,
163 "Verify packet arrives on correct interface");
165 struct pfil_head inet_pfil_hook; /* Packet filter hooks */
167 static struct ifqueue ipintrq;
168 static int ipqmaxlen = IFQ_MAXLEN;
170 extern struct domain inetdomain;
171 extern struct protosw inetsw[];
172 u_char ip_protox[IPPROTO_MAX];
174 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
175 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
176 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
177 &ipintrq.ifq_drops, 0,
178 "Number of packets dropped from the IP input queue");
180 SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
181 ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
183 #ifdef VIMAGE_GLOBALS
184 static uma_zone_t ipq_zone;
186 static struct mtx ipqlock;
188 #define IPQ_LOCK() mtx_lock(&ipqlock)
189 #define IPQ_UNLOCK() mtx_unlock(&ipqlock)
190 #define IPQ_LOCK_INIT() mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
191 #define IPQ_LOCK_ASSERT() mtx_assert(&ipqlock, MA_OWNED)
193 static void maxnipq_update(void);
194 static void ipq_zone_change(void *);
196 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, fragpackets,
198 "Current number of IPv4 fragment reassembly queue entries");
200 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, maxfragsperpacket,
201 CTLFLAG_RW, maxfragsperpacket, 0,
202 "Maximum number of IPv4 fragments allowed per packet");
204 struct callout ipport_tick_callout;
207 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
208 &ip_mtu, 0, "Default MTU");
212 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
213 ipstealth, 0, "IP stealth mode, no TTL decrementation on forwarding");
215 static int ip_output_flowtable_size = 2048;
216 TUNABLE_INT("net.inet.ip.output_flowtable_size", &ip_output_flowtable_size);
217 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, output_flowtable_size,
218 CTLFLAG_RDTUN, ip_output_flowtable_size, 2048,
219 "number of entries in the per-cpu output flow caches");
222 * ipfw_ether and ipfw_bridge hooks.
223 * XXX: Temporary until those are converted to pfil_hooks as well.
225 ip_fw_chk_t *ip_fw_chk_ptr = NULL;
226 ip_dn_io_t *ip_dn_io_ptr = NULL;
227 #ifdef VIMAGE_GLOBALS
230 struct flowtable *ip_ft;
232 static void ip_freef(struct ipqhead *, struct ipq *);
234 #ifndef VIMAGE_GLOBALS
235 static void vnet_inet_register(void);
237 static const vnet_modinfo_t vnet_inet_modinfo = {
238 .vmi_id = VNET_MOD_INET,
242 static void vnet_inet_register()
245 vnet_mod_register(&vnet_inet_modinfo);
248 SYSINIT(inet, SI_SUB_PROTO_BEGIN, SI_ORDER_FIRST, vnet_inet_register, 0);
252 * IP initialization: fill in IP protocol switch table.
253 * All protocols not implemented in kernel go to raw IP protocol handler.
258 INIT_VNET_INET(curvnet);
262 V_ipsendredirects = 1; /* XXX */
263 V_ip_checkinterface = 0;
265 V_ip_sendsourcequench = 0;
267 V_ip_defttl = IPDEFTTL;
268 V_ip_do_randomid = 0;
269 V_ip_id = time_second & 0xffff;
272 V_nipq = 0; /* Total # of reass queues */
274 V_ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
275 V_ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
276 V_ipport_firstauto = IPPORT_EPHEMERALFIRST; /* 10000 */
277 V_ipport_lastauto = IPPORT_EPHEMERALLAST; /* 65535 */
278 V_ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
279 V_ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
280 V_ipport_reservedhigh = IPPORT_RESERVED - 1; /* 1023 */
281 V_ipport_reservedlow = 0;
282 V_ipport_randomized = 1; /* user controlled via sysctl */
283 V_ipport_randomcps = 10; /* user controlled via sysctl */
284 V_ipport_randomtime = 45; /* user controlled via sysctl */
285 V_ipport_stoprandom = 0; /* toggled by ipport_tick */
290 /* XXX global static but not instantiated in this file */
291 V_ipfastforward_active = 0;
292 V_subnetsarelocal = 0;
293 V_sameprefixcarponly = 0;
296 TAILQ_INIT(&V_in_ifaddrhead);
297 V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
299 /* Initialize IP reassembly queue. */
300 for (i = 0; i < IPREASS_NHASH; i++)
301 TAILQ_INIT(&V_ipq[i]);
302 V_maxnipq = nmbclusters / 32;
303 V_maxfragsperpacket = 16;
304 V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
305 NULL, UMA_ALIGN_PTR, 0);
308 /* Skip initialization of globals for non-default instances. */
309 if (!IS_DEFAULT_VNET(curvnet))
312 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
314 panic("ip_init: PF_INET not found");
316 /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
317 for (i = 0; i < IPPROTO_MAX; i++)
318 ip_protox[i] = pr - inetsw;
320 * Cycle through IP protocols and put them into the appropriate place
323 for (pr = inetdomain.dom_protosw;
324 pr < inetdomain.dom_protoswNPROTOSW; pr++)
325 if (pr->pr_domain->dom_family == PF_INET &&
326 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
327 /* Be careful to only index valid IP protocols. */
328 if (pr->pr_protocol < IPPROTO_MAX)
329 ip_protox[pr->pr_protocol] = pr - inetsw;
332 /* Initialize packet filter hooks. */
333 inet_pfil_hook.ph_type = PFIL_TYPE_AF;
334 inet_pfil_hook.ph_af = AF_INET;
335 if ((i = pfil_head_register(&inet_pfil_hook)) != 0)
336 printf("%s: WARNING: unable to register pfil hook, "
337 "error %d\n", __func__, i);
339 /* Start ipport_tick. */
340 callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
342 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
343 SHUTDOWN_PRI_DEFAULT);
344 EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
345 NULL, EVENTHANDLER_PRI_ANY);
347 /* Initialize various other remaining things. */
349 ipintrq.ifq_maxlen = ipqmaxlen;
350 mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF);
351 netisr_register(NETISR_IP, ip_input, &ipintrq, 0);
353 ip_ft = flowtable_alloc(ip_output_flowtable_size, FL_PCPU);
360 callout_stop(&ipport_tick_callout);
364 * Ip input routine. Checksum and byte swap header. If fragmented
365 * try to reassemble. Process options. Pass to next level.
368 ip_input(struct mbuf *m)
370 INIT_VNET_INET(curvnet);
371 struct ip *ip = NULL;
372 struct in_ifaddr *ia = NULL;
375 int checkif, hlen = 0;
377 int dchg = 0; /* dest changed after fw */
378 struct in_addr odst; /* original dst address */
382 if (m->m_flags & M_FASTFWD_OURS) {
384 * Firewall or NAT changed destination to local.
385 * We expect ip_len and ip_off to be in host byte order.
387 m->m_flags &= ~M_FASTFWD_OURS;
388 /* Set up some basics that will be used later. */
389 ip = mtod(m, struct ip *);
390 hlen = ip->ip_hl << 2;
394 IPSTAT_INC(ips_total);
396 if (m->m_pkthdr.len < sizeof(struct ip))
399 if (m->m_len < sizeof (struct ip) &&
400 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
401 IPSTAT_INC(ips_toosmall);
404 ip = mtod(m, struct ip *);
406 if (ip->ip_v != IPVERSION) {
407 IPSTAT_INC(ips_badvers);
411 hlen = ip->ip_hl << 2;
412 if (hlen < sizeof(struct ip)) { /* minimum header length */
413 IPSTAT_INC(ips_badhlen);
416 if (hlen > m->m_len) {
417 if ((m = m_pullup(m, hlen)) == NULL) {
418 IPSTAT_INC(ips_badhlen);
421 ip = mtod(m, struct ip *);
424 /* 127/8 must not appear on wire - RFC1122 */
425 ifp = m->m_pkthdr.rcvif;
426 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
427 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
428 if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
429 IPSTAT_INC(ips_badaddr);
434 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
435 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
437 if (hlen == sizeof(struct ip)) {
438 sum = in_cksum_hdr(ip);
440 sum = in_cksum(m, hlen);
444 IPSTAT_INC(ips_badsum);
449 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
450 /* packet is dropped by traffic conditioner */
455 * Convert fields to host representation.
457 ip->ip_len = ntohs(ip->ip_len);
458 if (ip->ip_len < hlen) {
459 IPSTAT_INC(ips_badlen);
462 ip->ip_off = ntohs(ip->ip_off);
465 * Check that the amount of data in the buffers
466 * is as at least much as the IP header would have us expect.
467 * Trim mbufs if longer than we expect.
468 * Drop packet if shorter than we expect.
470 if (m->m_pkthdr.len < ip->ip_len) {
472 IPSTAT_INC(ips_tooshort);
475 if (m->m_pkthdr.len > ip->ip_len) {
476 if (m->m_len == m->m_pkthdr.len) {
477 m->m_len = ip->ip_len;
478 m->m_pkthdr.len = ip->ip_len;
480 m_adj(m, ip->ip_len - m->m_pkthdr.len);
484 * Bypass packet filtering for packets from a tunnel (gif).
486 if (ip_ipsec_filtertunnel(m))
491 * Run through list of hooks for input packets.
493 * NB: Beware of the destination address changing (e.g.
494 * by NAT rewriting). When this happens, tell
495 * ip_forward to do the right thing.
498 /* Jump over all PFIL processing if hooks are not active. */
499 if (!PFIL_HOOKED(&inet_pfil_hook))
503 if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0)
505 if (m == NULL) /* consumed by filter */
508 ip = mtod(m, struct ip *);
509 dchg = (odst.s_addr != ip->ip_dst.s_addr);
510 ifp = m->m_pkthdr.rcvif;
512 #ifdef IPFIREWALL_FORWARD
513 if (m->m_flags & M_FASTFWD_OURS) {
514 m->m_flags &= ~M_FASTFWD_OURS;
517 if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) {
519 * Directly ship on the packet. This allows to forward packets
520 * that were destined for us to some other directly connected
526 #endif /* IPFIREWALL_FORWARD */
530 * Process options and, if not destined for us,
531 * ship it on. ip_dooptions returns 1 when an
532 * error was detected (causing an icmp message
533 * to be sent and the original packet to be freed).
535 if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
538 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
539 * matter if it is destined to another node, or whether it is
540 * a multicast one, RSVP wants it! and prevents it from being forwarded
541 * anywhere else. Also checks if the rsvp daemon is running before
542 * grabbing the packet.
544 if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
548 * Check our list of addresses, to see if the packet is for us.
549 * If we don't have any addresses, assume any unicast packet
550 * we receive might be for us (and let the upper layers deal
553 if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
554 (m->m_flags & (M_MCAST|M_BCAST)) == 0)
558 * Enable a consistency check between the destination address
559 * and the arrival interface for a unicast packet (the RFC 1122
560 * strong ES model) if IP forwarding is disabled and the packet
561 * is not locally generated and the packet is not subject to
564 * XXX - Checking also should be disabled if the destination
565 * address is ipnat'ed to a different interface.
567 * XXX - Checking is incompatible with IP aliases added
568 * to the loopback interface instead of the interface where
569 * the packets are received.
571 * XXX - This is the case for carp vhost IPs as well so we
572 * insert a workaround. If the packet got here, we already
573 * checked with carp_iamatch() and carp_forus().
575 checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
576 ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
583 * Check for exact addresses in the hash bucket.
585 LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
587 * If the address matches, verify that the packet
588 * arrived via the correct interface if checking is
591 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
592 (!checkif || ia->ia_ifp == ifp))
596 * Check for broadcast addresses.
598 * Only accept broadcast packets that arrive via the matching
599 * interface. Reception of forwarded directed broadcasts would
600 * be handled via ip_forward() and ether_output() with the loopback
601 * into the stack for SIMPLEX interfaces handled by ether_output().
603 if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
605 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
606 if (ifa->ifa_addr->sa_family != AF_INET)
609 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
614 if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr) {
619 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
627 /* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
628 if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
629 IPSTAT_INC(ips_cantforward);
633 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
636 * If we are acting as a multicast router, all
637 * incoming multicast packets are passed to the
638 * kernel-level multicast forwarding function.
639 * The packet is returned (relatively) intact; if
640 * ip_mforward() returns a non-zero value, the packet
641 * must be discarded, else it may be accepted below.
643 if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
644 IPSTAT_INC(ips_cantforward);
650 * The process-level routing daemon needs to receive
651 * all multicast IGMP packets, whether or not this
652 * host belongs to their destination groups.
654 if (ip->ip_p == IPPROTO_IGMP)
656 IPSTAT_INC(ips_forward);
659 * Assume the packet is for us, to avoid prematurely taking
660 * a lock on the in_multi hash. Protocols must perform
661 * their own filtering and update statistics accordingly.
665 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
667 if (ip->ip_dst.s_addr == INADDR_ANY)
671 * FAITH(Firewall Aided Internet Translator)
673 if (ifp && ifp->if_type == IFT_FAITH) {
674 if (V_ip_keepfaith) {
675 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
683 * Not for us; forward if possible and desirable.
685 if (V_ipforwarding == 0) {
686 IPSTAT_INC(ips_cantforward);
700 * IPSTEALTH: Process non-routing options only
701 * if the packet is destined for us.
703 if (V_ipstealth && hlen > sizeof (struct ip) &&
706 #endif /* IPSTEALTH */
708 /* Count the packet in the ip address stats */
710 ia->ia_ifa.if_ipackets++;
711 ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
715 * Attempt reassembly; if it succeeds, proceed.
716 * ip_reass() will return a different mbuf.
718 if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
722 ip = mtod(m, struct ip *);
723 /* Get the header length of the reassembled packet */
724 hlen = ip->ip_hl << 2;
728 * Further protocols expect the packet length to be w/o the
735 * enforce IPsec policy checking if we are seeing last header.
736 * note that we do not visit this with protocols with pcb layer
737 * code - like udp/tcp/raw ip.
739 if (ip_ipsec_input(m))
744 * Switch out to protocol's input routine.
746 IPSTAT_INC(ips_delivered);
748 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
755 * After maxnipq has been updated, propagate the change to UMA. The UMA zone
756 * max has slightly different semantics than the sysctl, for historical
762 INIT_VNET_INET(curvnet);
765 * -1 for unlimited allocation.
768 uma_zone_set_max(V_ipq_zone, 0);
770 * Positive number for specific bound.
773 uma_zone_set_max(V_ipq_zone, V_maxnipq);
775 * Zero specifies no further fragment queue allocation -- set the
776 * bound very low, but rely on implementation elsewhere to actually
777 * prevent allocation and reclaim current queues.
780 uma_zone_set_max(V_ipq_zone, 1);
784 ipq_zone_change(void *tag)
786 INIT_VNET_INET(curvnet);
788 if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
789 V_maxnipq = nmbclusters / 32;
795 sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
797 INIT_VNET_INET(curvnet);
801 error = sysctl_handle_int(oidp, &i, 0, req);
802 if (error || !req->newptr)
806 * XXXRW: Might be a good idea to sanity check the argument and place
807 * an extreme upper bound.
816 SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
817 NULL, 0, sysctl_maxnipq, "I",
818 "Maximum number of IPv4 fragment reassembly queue entries");
821 * Take incoming datagram fragment and try to reassemble it into
822 * whole datagram. If the argument is the first fragment or one
823 * in between the function will return NULL and store the mbuf
824 * in the fragment chain. If the argument is the last fragment
825 * the packet will be reassembled and the pointer to the new
826 * mbuf returned for further processing. Only m_tags attached
827 * to the first packet/fragment are preserved.
828 * The IP header is *NOT* adjusted out of iplen.
831 ip_reass(struct mbuf *m)
833 INIT_VNET_INET(curvnet);
835 struct mbuf *p, *q, *nq, *t;
836 struct ipq *fp = NULL;
837 struct ipqhead *head;
842 /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
843 if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
844 IPSTAT_INC(ips_fragments);
845 IPSTAT_INC(ips_fragdropped);
850 ip = mtod(m, struct ip *);
851 hlen = ip->ip_hl << 2;
853 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
858 * Look for queue of fragments
861 TAILQ_FOREACH(fp, head, ipq_list)
862 if (ip->ip_id == fp->ipq_id &&
863 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
864 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
866 mac_ipq_match(m, fp) &&
868 ip->ip_p == fp->ipq_p)
874 * Attempt to trim the number of allocated fragment queues if it
875 * exceeds the administrative limit.
877 if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
879 * drop something from the tail of the current queue
880 * before proceeding further
882 struct ipq *q = TAILQ_LAST(head, ipqhead);
883 if (q == NULL) { /* gak */
884 for (i = 0; i < IPREASS_NHASH; i++) {
885 struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead);
887 IPSTAT_ADD(ips_fragtimeout,
889 ip_freef(&V_ipq[i], r);
894 IPSTAT_ADD(ips_fragtimeout, q->ipq_nfrags);
901 * Adjust ip_len to not reflect header,
902 * convert offset of this to bytes.
905 if (ip->ip_off & IP_MF) {
907 * Make sure that fragments have a data length
908 * that's a non-zero multiple of 8 bytes.
910 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
911 IPSTAT_INC(ips_toosmall); /* XXX */
914 m->m_flags |= M_FRAG;
916 m->m_flags &= ~M_FRAG;
921 * Attempt reassembly; if it succeeds, proceed.
922 * ip_reass() will return a different mbuf.
924 IPSTAT_INC(ips_fragments);
925 m->m_pkthdr.header = ip;
927 /* Previous ip_reass() started here. */
929 * Presence of header sizes in mbufs
930 * would confuse code below.
936 * If first fragment to arrive, create a reassembly queue.
939 fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
943 if (mac_ipq_init(fp, M_NOWAIT) != 0) {
944 uma_zfree(V_ipq_zone, fp);
948 mac_ipq_create(m, fp);
950 TAILQ_INSERT_HEAD(head, fp, ipq_list);
953 fp->ipq_ttl = IPFRAGTTL;
954 fp->ipq_p = ip->ip_p;
955 fp->ipq_id = ip->ip_id;
956 fp->ipq_src = ip->ip_src;
957 fp->ipq_dst = ip->ip_dst;
964 mac_ipq_update(m, fp);
968 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
971 * Handle ECN by comparing this segment with the first one;
972 * if CE is set, do not lose CE.
973 * drop if CE and not-ECT are mixed for the same packet.
975 ecn = ip->ip_tos & IPTOS_ECN_MASK;
976 ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
977 if (ecn == IPTOS_ECN_CE) {
978 if (ecn0 == IPTOS_ECN_NOTECT)
980 if (ecn0 != IPTOS_ECN_CE)
981 GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
983 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
987 * Find a segment which begins after this one does.
989 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
990 if (GETIP(q)->ip_off > ip->ip_off)
994 * If there is a preceding segment, it may provide some of
995 * our data already. If so, drop the data from the incoming
996 * segment. If it provides all of our data, drop us, otherwise
997 * stick new segment in the proper place.
999 * If some of the data is dropped from the the preceding
1000 * segment, then it's checksum is invalidated.
1003 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
1005 if (i >= ip->ip_len)
1008 m->m_pkthdr.csum_flags = 0;
1012 m->m_nextpkt = p->m_nextpkt;
1015 m->m_nextpkt = fp->ipq_frags;
1020 * While we overlap succeeding segments trim them or,
1021 * if they are completely covered, dequeue them.
1023 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
1025 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
1026 if (i < GETIP(q)->ip_len) {
1027 GETIP(q)->ip_len -= i;
1028 GETIP(q)->ip_off += i;
1030 q->m_pkthdr.csum_flags = 0;
1035 IPSTAT_INC(ips_fragdropped);
1041 * Check for complete reassembly and perform frag per packet
1044 * Frag limiting is performed here so that the nth frag has
1045 * a chance to complete the packet before we drop the packet.
1046 * As a result, n+1 frags are actually allowed per packet, but
1047 * only n will ever be stored. (n = maxfragsperpacket.)
1051 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1052 if (GETIP(q)->ip_off != next) {
1053 if (fp->ipq_nfrags > V_maxfragsperpacket) {
1054 IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1059 next += GETIP(q)->ip_len;
1061 /* Make sure the last packet didn't have the IP_MF flag */
1062 if (p->m_flags & M_FRAG) {
1063 if (fp->ipq_nfrags > V_maxfragsperpacket) {
1064 IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1071 * Reassembly is complete. Make sure the packet is a sane size.
1075 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
1076 IPSTAT_INC(ips_toolong);
1077 IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1083 * Concatenate fragments.
1090 q->m_nextpkt = NULL;
1091 for (q = nq; q != NULL; q = nq) {
1093 q->m_nextpkt = NULL;
1094 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1095 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1099 * In order to do checksumming faster we do 'end-around carry' here
1100 * (and not in for{} loop), though it implies we are not going to
1101 * reassemble more than 64k fragments.
1103 m->m_pkthdr.csum_data =
1104 (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16);
1106 mac_ipq_reassemble(fp, m);
1107 mac_ipq_destroy(fp);
1111 * Create header for new ip packet by modifying header of first
1112 * packet; dequeue and discard fragment reassembly header.
1113 * Make header visible.
1115 ip->ip_len = (ip->ip_hl << 2) + next;
1116 ip->ip_src = fp->ipq_src;
1117 ip->ip_dst = fp->ipq_dst;
1118 TAILQ_REMOVE(head, fp, ipq_list);
1120 uma_zfree(V_ipq_zone, fp);
1121 m->m_len += (ip->ip_hl << 2);
1122 m->m_data -= (ip->ip_hl << 2);
1123 /* some debugging cruft by sklower, below, will go away soon */
1124 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */
1126 IPSTAT_INC(ips_reassembled);
1131 IPSTAT_INC(ips_fragdropped);
1143 * Free a fragment reassembly header and all
1144 * associated datagrams.
1147 ip_freef(struct ipqhead *fhp, struct ipq *fp)
1149 INIT_VNET_INET(curvnet);
1154 while (fp->ipq_frags) {
1156 fp->ipq_frags = q->m_nextpkt;
1159 TAILQ_REMOVE(fhp, fp, ipq_list);
1160 uma_zfree(V_ipq_zone, fp);
1165 * IP timer processing;
1166 * if a timer expires on a reassembly
1167 * queue, discard it.
1172 VNET_ITERATOR_DECL(vnet_iter);
1178 VNET_FOREACH(vnet_iter) {
1179 CURVNET_SET(vnet_iter);
1180 INIT_VNET_INET(vnet_iter);
1181 for (i = 0; i < IPREASS_NHASH; i++) {
1182 for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) {
1186 fp = TAILQ_NEXT(fp, ipq_list);
1187 if(--fpp->ipq_ttl == 0) {
1188 IPSTAT_ADD(ips_fragtimeout,
1190 ip_freef(&V_ipq[i], fpp);
1195 * If we are over the maximum number of fragments
1196 * (due to the limit being lowered), drain off
1197 * enough to get down to the new limit.
1199 if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1200 for (i = 0; i < IPREASS_NHASH; i++) {
1201 while (V_nipq > V_maxnipq &&
1202 !TAILQ_EMPTY(&V_ipq[i])) {
1203 IPSTAT_ADD(ips_fragdropped,
1204 TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
1206 TAILQ_FIRST(&V_ipq[i]));
1212 VNET_LIST_RUNLOCK();
1217 * Drain off all datagram fragments.
1222 VNET_ITERATOR_DECL(vnet_iter);
1227 VNET_FOREACH(vnet_iter) {
1228 CURVNET_SET(vnet_iter);
1229 INIT_VNET_INET(vnet_iter);
1230 for (i = 0; i < IPREASS_NHASH; i++) {
1231 while(!TAILQ_EMPTY(&V_ipq[i])) {
1232 IPSTAT_ADD(ips_fragdropped,
1233 TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
1234 ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1239 VNET_LIST_RUNLOCK();
1245 * The protocol to be inserted into ip_protox[] must be already registered
1246 * in inetsw[], either statically or through pf_proto_register().
1249 ipproto_register(u_char ipproto)
1253 /* Sanity checks. */
1255 return (EPROTONOSUPPORT);
1258 * The protocol slot must not be occupied by another protocol
1259 * already. An index pointing to IPPROTO_RAW is unused.
1261 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1263 return (EPFNOSUPPORT);
1264 if (ip_protox[ipproto] != pr - inetsw) /* IPPROTO_RAW */
1267 /* Find the protocol position in inetsw[] and set the index. */
1268 for (pr = inetdomain.dom_protosw;
1269 pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1270 if (pr->pr_domain->dom_family == PF_INET &&
1271 pr->pr_protocol && pr->pr_protocol == ipproto) {
1272 /* Be careful to only index valid IP protocols. */
1273 if (pr->pr_protocol < IPPROTO_MAX) {
1274 ip_protox[pr->pr_protocol] = pr - inetsw;
1280 return (EPROTONOSUPPORT);
1284 ipproto_unregister(u_char ipproto)
1288 /* Sanity checks. */
1290 return (EPROTONOSUPPORT);
1292 /* Check if the protocol was indeed registered. */
1293 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1295 return (EPFNOSUPPORT);
1296 if (ip_protox[ipproto] == pr - inetsw) /* IPPROTO_RAW */
1299 /* Reset the protocol slot to IPPROTO_RAW. */
1300 ip_protox[ipproto] = pr - inetsw;
1305 * Given address of next destination (final or next hop),
1306 * return internet address info of interface to be used to get there.
1309 ip_rtaddr(struct in_addr dst, u_int fibnum)
1312 struct sockaddr_in *sin;
1313 struct in_ifaddr *ifa;
1315 bzero(&sro, sizeof(sro));
1316 sin = (struct sockaddr_in *)&sro.ro_dst;
1317 sin->sin_family = AF_INET;
1318 sin->sin_len = sizeof(*sin);
1319 sin->sin_addr = dst;
1320 in_rtalloc_ign(&sro, 0, fibnum);
1322 if (sro.ro_rt == NULL)
1325 ifa = ifatoia(sro.ro_rt->rt_ifa);
1330 u_char inetctlerrmap[PRC_NCMDS] = {
1332 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1333 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1334 EMSGSIZE, EHOSTUNREACH, 0, 0,
1335 0, 0, EHOSTUNREACH, 0,
1336 ENOPROTOOPT, ECONNREFUSED
1340 * Forward a packet. If some error occurs return the sender
1341 * an icmp packet. Note we can't always generate a meaningful
1342 * icmp message because icmp doesn't have a large enough repertoire
1343 * of codes and types.
1345 * If not forwarding, just drop the packet. This could be confusing
1346 * if ipforwarding was zero but some routing protocol was advancing
1347 * us as a gateway to somewhere. However, we must let the routing
1348 * protocol deal with that.
1350 * The srcrt parameter indicates whether the packet is being forwarded
1351 * via a source route.
1354 ip_forward(struct mbuf *m, int srcrt)
1356 INIT_VNET_INET(curvnet);
1357 struct ip *ip = mtod(m, struct ip *);
1358 struct in_ifaddr *ia = NULL;
1360 struct in_addr dest;
1362 int error, type = 0, code = 0, mtu = 0;
1364 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1365 IPSTAT_INC(ips_cantforward);
1372 if (ip->ip_ttl <= IPTTLDEC) {
1373 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1381 ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1382 if (!srcrt && ia == NULL) {
1383 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1388 * Save the IP header and at most 8 bytes of the payload,
1389 * in case we need to generate an ICMP message to the src.
1391 * XXX this can be optimized a lot by saving the data in a local
1392 * buffer on the stack (72 bytes at most), and only allocating the
1393 * mbuf if really necessary. The vast majority of the packets
1394 * are forwarded without having to send an ICMP back (either
1395 * because unnecessary, or because rate limited), so we are
1396 * really we are wasting a lot of work here.
1398 * We don't use m_copy() because it might return a reference
1399 * to a shared cluster. Both this function and ip_output()
1400 * assume exclusive access to the IP header in `m', so any
1401 * data in a cluster may change before we reach icmp_error().
1403 MGETHDR(mcopy, M_DONTWAIT, m->m_type);
1404 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
1406 * It's probably ok if the pkthdr dup fails (because
1407 * the deep copy of the tag chain failed), but for now
1408 * be conservative and just discard the copy since
1409 * code below may some day want the tags.
1414 if (mcopy != NULL) {
1415 mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy));
1416 mcopy->m_pkthdr.len = mcopy->m_len;
1417 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1423 ip->ip_ttl -= IPTTLDEC;
1429 * If forwarding packet using same interface that it came in on,
1430 * perhaps should send a redirect to sender to shortcut a hop.
1431 * Only send redirect if source is sending directly to us,
1432 * and if packet was not source routed (or has any options).
1433 * Also, don't send redirect if forwarding using a default route
1434 * or a route modified by a redirect.
1437 if (!srcrt && V_ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) {
1438 struct sockaddr_in *sin;
1441 bzero(&ro, sizeof(ro));
1442 sin = (struct sockaddr_in *)&ro.ro_dst;
1443 sin->sin_family = AF_INET;
1444 sin->sin_len = sizeof(*sin);
1445 sin->sin_addr = ip->ip_dst;
1446 in_rtalloc_ign(&ro, 0, M_GETFIB(m));
1450 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1451 satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1452 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1453 u_long src = ntohl(ip->ip_src.s_addr);
1456 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1457 if (rt->rt_flags & RTF_GATEWAY)
1458 dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1460 dest.s_addr = ip->ip_dst.s_addr;
1461 /* Router requirements says to only send host redirects */
1462 type = ICMP_REDIRECT;
1463 code = ICMP_REDIRECT_HOST;
1471 * Try to cache the route MTU from ip_output so we can consider it for
1472 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1474 bzero(&ro, sizeof(ro));
1476 error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1478 if (error == EMSGSIZE && ro.ro_rt)
1479 mtu = ro.ro_rt->rt_rmx.rmx_mtu;
1484 IPSTAT_INC(ips_cantforward);
1486 IPSTAT_INC(ips_forward);
1488 IPSTAT_INC(ips_redirectsent);
1500 case 0: /* forwarded, but need redirect */
1501 /* type, code set above */
1504 case ENETUNREACH: /* shouldn't happen, checked above */
1509 type = ICMP_UNREACH;
1510 code = ICMP_UNREACH_HOST;
1514 type = ICMP_UNREACH;
1515 code = ICMP_UNREACH_NEEDFRAG;
1519 * If IPsec is configured for this path,
1520 * override any possibly mtu value set by ip_output.
1522 mtu = ip_ipsec_mtu(m, mtu);
1525 * If the MTU was set before make sure we are below the
1527 * If the MTU wasn't set before use the interface mtu or
1528 * fall back to the next smaller mtu step compared to the
1529 * current packet size.
1533 mtu = min(mtu, ia->ia_ifp->if_mtu);
1536 mtu = ia->ia_ifp->if_mtu;
1538 mtu = ip_next_mtu(ip->ip_len, 0);
1540 IPSTAT_INC(ips_cantfrag);
1545 * A router should not generate ICMP_SOURCEQUENCH as
1546 * required in RFC1812 Requirements for IP Version 4 Routers.
1547 * Source quench could be a big problem under DoS attacks,
1548 * or if the underlying interface is rate-limited.
1549 * Those who need source quench packets may re-enable them
1550 * via the net.inet.ip.sendsourcequench sysctl.
1552 if (V_ip_sendsourcequench == 0) {
1556 type = ICMP_SOURCEQUENCH;
1561 case EACCES: /* ipfw denied packet */
1565 icmp_error(mcopy, type, code, dest.s_addr, mtu);
1569 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1572 INIT_VNET_NET(inp->inp_vnet);
1574 if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1578 if (inp->inp_socket->so_options & SO_BINTIME) {
1579 *mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt),
1580 SCM_BINTIME, SOL_SOCKET);
1582 mp = &(*mp)->m_next;
1584 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1587 bintime2timeval(&bt, &tv);
1588 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1589 SCM_TIMESTAMP, SOL_SOCKET);
1591 mp = &(*mp)->m_next;
1594 if (inp->inp_flags & INP_RECVDSTADDR) {
1595 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1596 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1598 mp = &(*mp)->m_next;
1600 if (inp->inp_flags & INP_RECVTTL) {
1601 *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
1602 sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
1604 mp = &(*mp)->m_next;
1608 * Moving these out of udp_input() made them even more broken
1609 * than they already were.
1611 /* options were tossed already */
1612 if (inp->inp_flags & INP_RECVOPTS) {
1613 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1614 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1616 mp = &(*mp)->m_next;
1618 /* ip_srcroute doesn't do what we want here, need to fix */
1619 if (inp->inp_flags & INP_RECVRETOPTS) {
1620 *mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
1621 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1623 mp = &(*mp)->m_next;
1626 if (inp->inp_flags & INP_RECVIF) {
1629 struct sockaddr_dl sdl;
1632 struct sockaddr_dl *sdp;
1633 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1635 if (((ifp = m->m_pkthdr.rcvif))
1636 && ( ifp->if_index && (ifp->if_index <= V_if_index))) {
1637 sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1639 * Change our mind and don't try copy.
1641 if ((sdp->sdl_family != AF_LINK)
1642 || (sdp->sdl_len > sizeof(sdlbuf))) {
1645 bcopy(sdp, sdl2, sdp->sdl_len);
1649 = offsetof(struct sockaddr_dl, sdl_data[0]);
1650 sdl2->sdl_family = AF_LINK;
1651 sdl2->sdl_index = 0;
1652 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1654 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
1655 IP_RECVIF, IPPROTO_IP);
1657 mp = &(*mp)->m_next;
1662 * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
1663 * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
1664 * locking. This code remains in ip_input.c as ip_mroute.c is optionally
1668 ip_rsvp_init(struct socket *so)
1670 INIT_VNET_INET(so->so_vnet);
1672 if (so->so_type != SOCK_RAW ||
1673 so->so_proto->pr_protocol != IPPROTO_RSVP)
1676 if (V_ip_rsvpd != NULL)
1681 * This may seem silly, but we need to be sure we don't over-increment
1682 * the RSVP counter, in case something slips up.
1684 if (!V_ip_rsvp_on) {
1695 INIT_VNET_INET(curvnet);
1699 * This may seem silly, but we need to be sure we don't over-decrement
1700 * the RSVP counter, in case something slips up.
1710 rsvp_input(struct mbuf *m, int off) /* XXX must fixup manually */
1712 INIT_VNET_INET(curvnet);
1714 if (rsvp_input_p) { /* call the real one if loaded */
1715 rsvp_input_p(m, off);
1719 /* Can still get packets with rsvp_on = 0 if there is a local member
1720 * of the group to which the RSVP packet is addressed. But in this
1721 * case we want to throw the packet away.
1729 if (V_ip_rsvpd != NULL) {
1733 /* Drop the packet */