]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/ip_input.c
Allow the kernel to boot on a CPU where the devicetree has numbered it with
[FreeBSD/FreeBSD.git] / sys / netinet / ip_input.c
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  *      @(#)ip_input.c  8.2 (Berkeley) 1/4/94
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_bootp.h"
36 #include "opt_ipfw.h"
37 #include "opt_ipstealth.h"
38 #include "opt_ipsec.h"
39 #include "opt_route.h"
40 #include "opt_rss.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/hhook.h>
45 #include <sys/mbuf.h>
46 #include <sys/malloc.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/time.h>
51 #include <sys/kernel.h>
52 #include <sys/lock.h>
53 #include <sys/rmlock.h>
54 #include <sys/rwlock.h>
55 #include <sys/sdt.h>
56 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
58
59 #include <net/pfil.h>
60 #include <net/if.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>
67 #include <net/vnet.h>
68
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>
81 #ifdef IPSEC
82 #include <netinet/ip_ipsec.h>
83 #include <netipsec/ipsec.h>
84 #include <netipsec/key.h>
85 #endif /* IPSEC */
86 #include <netinet/in_rss.h>
87
88 #include <sys/socketvar.h>
89
90 #include <security/mac/mac_framework.h>
91
92 #ifdef CTASSERT
93 CTASSERT(sizeof(struct ip) == 20);
94 #endif
95
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);
100 #ifdef VIMAGE
101 extern void ipreass_destroy(void);
102 #endif
103
104 struct rmlock in_ifaddr_lock;
105 RM_SYSINIT(in_ifaddr_lock, &in_ifaddr_lock, "in_ifaddr_lock");
106
107 VNET_DEFINE(int, rsvp_on);
108
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");
113
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");
119
120 /*
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.
125  *
126  * XXX - ip_checkinterface currently must be disabled if you use ipnat
127  * to translate the destination address to another local interface.
128  *
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.
132  */
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");
138
139 VNET_DEFINE(struct pfil_head, inet_pfil_hook);  /* Packet filter hooks */
140
141 static struct netisr_handler ip_nh = {
142         .nh_name = "ip",
143         .nh_handler = ip_input,
144         .nh_proto = NETISR_IP,
145 #ifdef  RSS
146         .nh_m2cpuid = rss_soft_m2cpuid_v4,
147         .nh_policy = NETISR_POLICY_CPU,
148         .nh_dispatch = NETISR_DISPATCH_HYBRID,
149 #else
150         .nh_policy = NETISR_POLICY_FLOW,
151 #endif
152 };
153
154 #ifdef  RSS
155 /*
156  * Directly dispatched frames are currently assumed
157  * to have a flowid already calculated.
158  *
159  * It should likely have something that assert it
160  * actually has valid flow details.
161  */
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,
169 };
170 #endif
171
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 */
178
179 #ifdef IPCTL_DEFMTU
180 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
181     &ip_mtu, 0, "Default MTU");
182 #endif
183
184 #ifdef IPSTEALTH
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");
189 #endif
190
191 /*
192  * IP statistics are stored in the "array" of counter(9)s.
193  */
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)");
198
199 #ifdef VIMAGE
200 VNET_PCPUSTAT_SYSUNINIT(ipstat);
201 #endif /* VIMAGE */
202
203 /*
204  * Kernel module interface for updating ipstat.  The argument is an index
205  * into ipstat treated as an array.
206  */
207 void
208 kmod_ipstat_inc(int statnum)
209 {
210
211         counter_u64_add(VNET(ipstat)[statnum], 1);
212 }
213
214 void
215 kmod_ipstat_dec(int statnum)
216 {
217
218         counter_u64_add(VNET(ipstat)[statnum], -1);
219 }
220
221 static int
222 sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
223 {
224         int error, qlimit;
225
226         netisr_getqlimit(&ip_nh, &qlimit);
227         error = sysctl_handle_int(oidp, &qlimit, 0, req);
228         if (error || !req->newptr)
229                 return (error);
230         if (qlimit < 1)
231                 return (EINVAL);
232         return (netisr_setqlimit(&ip_nh, qlimit));
233 }
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");
237
238 static int
239 sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
240 {
241         u_int64_t qdrops_long;
242         int error, qdrops;
243
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)
248                 return (error);
249         if (qdrops != 0)
250                 return (EINVAL);
251         netisr_clearqdrops(&ip_nh);
252         return (0);
253 }
254
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");
258
259 #ifdef  RSS
260 static int
261 sysctl_netinet_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
262 {
263         int error, qlimit;
264
265         netisr_getqlimit(&ip_direct_nh, &qlimit);
266         error = sysctl_handle_int(oidp, &qlimit, 0, req);
267         if (error || !req->newptr)
268                 return (error);
269         if (qlimit < 1)
270                 return (EINVAL);
271         return (netisr_setqlimit(&ip_direct_nh, qlimit));
272 }
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");
276
277 static int
278 sysctl_netinet_intr_direct_queue_drops(SYSCTL_HANDLER_ARGS)
279 {
280         u_int64_t qdrops_long;
281         int error, qdrops;
282
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)
287                 return (error);
288         if (qdrops != 0)
289                 return (EINVAL);
290         netisr_clearqdrops(&ip_direct_nh);
291         return (0);
292 }
293
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");
297 #endif  /* RSS */
298
299 /*
300  * IP initialization: fill in IP protocol switch table.
301  * All protocols not implemented in kernel go to raw IP protocol handler.
302  */
303 void
304 ip_init(void)
305 {
306         struct protosw *pr;
307         int i;
308
309         TAILQ_INIT(&V_in_ifaddrhead);
310         V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
311
312         /* Initialize IP reassembly queue. */
313         ipreass_init();
314
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);
321
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",
326                     __func__);
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",
331                     __func__);
332
333         /* Skip initialization of globals for non-default instances. */
334         if (!IS_DEFAULT_VNET(curvnet))
335                 return;
336
337         pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
338         if (pr == NULL)
339                 panic("ip_init: PF_INET not found");
340
341         /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
342         for (i = 0; i < IPPROTO_MAX; i++)
343                 ip_protox[i] = pr - inetsw;
344         /*
345          * Cycle through IP protocols and put them into the appropriate place
346          * in ip_protox[].
347          */
348         for (pr = inetdomain.dom_protosw;
349             pr < inetdomain.dom_protoswNPROTOSW; pr++)
350                 if (pr->pr_domain->dom_family == PF_INET &&
351                     pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
352                         /* Be careful to only index valid IP protocols. */
353                         if (pr->pr_protocol < IPPROTO_MAX)
354                                 ip_protox[pr->pr_protocol] = pr - inetsw;
355                 }
356
357         netisr_register(&ip_nh);
358 #ifdef  RSS
359         netisr_register(&ip_direct_nh);
360 #endif
361 }
362
363 #ifdef VIMAGE
364 void
365 ip_destroy(void)
366 {
367         int error;
368
369         if ((error = pfil_head_unregister(&V_inet_pfil_hook)) != 0)
370                 printf("%s: WARNING: unable to unregister pfil hook, "
371                     "error %d\n", __func__, error);
372
373         error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET]);
374         if (error != 0) {
375                 printf("%s: WARNING: unable to deregister input helper hook "
376                     "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET: "
377                     "error %d returned\n", __func__, error);
378         }
379         error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET]);
380         if (error != 0) {
381                 printf("%s: WARNING: unable to deregister output helper hook "
382                     "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET: "
383                     "error %d returned\n", __func__, error);
384         }
385         /* Cleanup in_ifaddr hash table; should be empty. */
386         hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask);
387
388         /* Destroy IP reassembly queue. */
389         ipreass_destroy();
390 }
391 #endif
392
393 #ifdef  RSS
394 /*
395  * IP direct input routine.
396  *
397  * This is called when reinjecting completed fragments where
398  * all of the previous checking and book-keeping has been done.
399  */
400 void
401 ip_direct_input(struct mbuf *m)
402 {
403         struct ip *ip;
404         int hlen;
405
406         ip = mtod(m, struct ip *);
407         hlen = ip->ip_hl << 2;
408
409         IPSTAT_INC(ips_delivered);
410         (*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p);
411         return;
412 }
413 #endif
414
415 /*
416  * Ip input routine.  Checksum and byte swap header.  If fragmented
417  * try to reassemble.  Process options.  Pass to next level.
418  */
419 void
420 ip_input(struct mbuf *m)
421 {
422         struct ip *ip = NULL;
423         struct in_ifaddr *ia = NULL;
424         struct ifaddr *ifa;
425         struct ifnet *ifp;
426         int    checkif, hlen = 0;
427         uint16_t sum, ip_len;
428         int dchg = 0;                           /* dest changed after fw */
429         struct in_addr odst;                    /* original dst address */
430
431         M_ASSERTPKTHDR(m);
432
433         if (m->m_flags & M_FASTFWD_OURS) {
434                 m->m_flags &= ~M_FASTFWD_OURS;
435                 /* Set up some basics that will be used later. */
436                 ip = mtod(m, struct ip *);
437                 hlen = ip->ip_hl << 2;
438                 ip_len = ntohs(ip->ip_len);
439                 goto ours;
440         }
441
442         IPSTAT_INC(ips_total);
443
444         if (m->m_pkthdr.len < sizeof(struct ip))
445                 goto tooshort;
446
447         if (m->m_len < sizeof (struct ip) &&
448             (m = m_pullup(m, sizeof (struct ip))) == NULL) {
449                 IPSTAT_INC(ips_toosmall);
450                 return;
451         }
452         ip = mtod(m, struct ip *);
453
454         if (ip->ip_v != IPVERSION) {
455                 IPSTAT_INC(ips_badvers);
456                 goto bad;
457         }
458
459         hlen = ip->ip_hl << 2;
460         if (hlen < sizeof(struct ip)) { /* minimum header length */
461                 IPSTAT_INC(ips_badhlen);
462                 goto bad;
463         }
464         if (hlen > m->m_len) {
465                 if ((m = m_pullup(m, hlen)) == NULL) {
466                         IPSTAT_INC(ips_badhlen);
467                         return;
468                 }
469                 ip = mtod(m, struct ip *);
470         }
471
472         IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL);
473
474         /* 127/8 must not appear on wire - RFC1122 */
475         ifp = m->m_pkthdr.rcvif;
476         if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
477             (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
478                 if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
479                         IPSTAT_INC(ips_badaddr);
480                         goto bad;
481                 }
482         }
483
484         if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
485                 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
486         } else {
487                 if (hlen == sizeof(struct ip)) {
488                         sum = in_cksum_hdr(ip);
489                 } else {
490                         sum = in_cksum(m, hlen);
491                 }
492         }
493         if (sum) {
494                 IPSTAT_INC(ips_badsum);
495                 goto bad;
496         }
497
498 #ifdef ALTQ
499         if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
500                 /* packet is dropped by traffic conditioner */
501                 return;
502 #endif
503
504         ip_len = ntohs(ip->ip_len);
505         if (ip_len < hlen) {
506                 IPSTAT_INC(ips_badlen);
507                 goto bad;
508         }
509
510         /*
511          * Check that the amount of data in the buffers
512          * is as at least much as the IP header would have us expect.
513          * Trim mbufs if longer than we expect.
514          * Drop packet if shorter than we expect.
515          */
516         if (m->m_pkthdr.len < ip_len) {
517 tooshort:
518                 IPSTAT_INC(ips_tooshort);
519                 goto bad;
520         }
521         if (m->m_pkthdr.len > ip_len) {
522                 if (m->m_len == m->m_pkthdr.len) {
523                         m->m_len = ip_len;
524                         m->m_pkthdr.len = ip_len;
525                 } else
526                         m_adj(m, ip_len - m->m_pkthdr.len);
527         }
528
529         /* Try to forward the packet, but if we fail continue */
530 #ifdef IPSEC
531         /* For now we do not handle IPSEC in tryforward. */
532         if (!key_havesp(IPSEC_DIR_INBOUND) && !key_havesp(IPSEC_DIR_OUTBOUND) &&
533             (V_ipforwarding == 1))
534                 if (ip_tryforward(m) == NULL)
535                         return;
536         /*
537          * Bypass packet filtering for packets previously handled by IPsec.
538          */
539         if (ip_ipsec_filtertunnel(m))
540                 goto passin;
541 #else
542         if (V_ipforwarding == 1)
543                 if (ip_tryforward(m) == NULL)
544                         return;
545 #endif /* IPSEC */
546
547         /*
548          * Run through list of hooks for input packets.
549          *
550          * NB: Beware of the destination address changing (e.g.
551          *     by NAT rewriting).  When this happens, tell
552          *     ip_forward to do the right thing.
553          */
554
555         /* Jump over all PFIL processing if hooks are not active. */
556         if (!PFIL_HOOKED(&V_inet_pfil_hook))
557                 goto passin;
558
559         odst = ip->ip_dst;
560         if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0)
561                 return;
562         if (m == NULL)                  /* consumed by filter */
563                 return;
564
565         ip = mtod(m, struct ip *);
566         dchg = (odst.s_addr != ip->ip_dst.s_addr);
567         ifp = m->m_pkthdr.rcvif;
568
569         if (m->m_flags & M_FASTFWD_OURS) {
570                 m->m_flags &= ~M_FASTFWD_OURS;
571                 goto ours;
572         }
573         if (m->m_flags & M_IP_NEXTHOP) {
574                 if (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
575                         /*
576                          * Directly ship the packet on.  This allows
577                          * forwarding packets originally destined to us
578                          * to some other directly connected host.
579                          */
580                         ip_forward(m, 1);
581                         return;
582                 }
583         }
584 passin:
585
586         /*
587          * Process options and, if not destined for us,
588          * ship it on.  ip_dooptions returns 1 when an
589          * error was detected (causing an icmp message
590          * to be sent and the original packet to be freed).
591          */
592         if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
593                 return;
594
595         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
596          * matter if it is destined to another node, or whether it is 
597          * a multicast one, RSVP wants it! and prevents it from being forwarded
598          * anywhere else. Also checks if the rsvp daemon is running before
599          * grabbing the packet.
600          */
601         if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP) 
602                 goto ours;
603
604         /*
605          * Check our list of addresses, to see if the packet is for us.
606          * If we don't have any addresses, assume any unicast packet
607          * we receive might be for us (and let the upper layers deal
608          * with it).
609          */
610         if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
611             (m->m_flags & (M_MCAST|M_BCAST)) == 0)
612                 goto ours;
613
614         /*
615          * Enable a consistency check between the destination address
616          * and the arrival interface for a unicast packet (the RFC 1122
617          * strong ES model) if IP forwarding is disabled and the packet
618          * is not locally generated and the packet is not subject to
619          * 'ipfw fwd'.
620          *
621          * XXX - Checking also should be disabled if the destination
622          * address is ipnat'ed to a different interface.
623          *
624          * XXX - Checking is incompatible with IP aliases added
625          * to the loopback interface instead of the interface where
626          * the packets are received.
627          *
628          * XXX - This is the case for carp vhost IPs as well so we
629          * insert a workaround. If the packet got here, we already
630          * checked with carp_iamatch() and carp_forus().
631          */
632         checkif = V_ip_checkinterface && (V_ipforwarding == 0) && 
633             ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
634             ifp->if_carp == NULL && (dchg == 0);
635
636         /*
637          * Check for exact addresses in the hash bucket.
638          */
639         /* IN_IFADDR_RLOCK(); */
640         LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
641                 /*
642                  * If the address matches, verify that the packet
643                  * arrived via the correct interface if checking is
644                  * enabled.
645                  */
646                 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr && 
647                     (!checkif || ia->ia_ifp == ifp)) {
648                         counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
649                         counter_u64_add(ia->ia_ifa.ifa_ibytes,
650                             m->m_pkthdr.len);
651                         /* IN_IFADDR_RUNLOCK(); */
652                         goto ours;
653                 }
654         }
655         /* IN_IFADDR_RUNLOCK(); */
656
657         /*
658          * Check for broadcast addresses.
659          *
660          * Only accept broadcast packets that arrive via the matching
661          * interface.  Reception of forwarded directed broadcasts would
662          * be handled via ip_forward() and ether_output() with the loopback
663          * into the stack for SIMPLEX interfaces handled by ether_output().
664          */
665         if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
666                 IF_ADDR_RLOCK(ifp);
667                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
668                         if (ifa->ifa_addr->sa_family != AF_INET)
669                                 continue;
670                         ia = ifatoia(ifa);
671                         if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
672                             ip->ip_dst.s_addr) {
673                                 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
674                                 counter_u64_add(ia->ia_ifa.ifa_ibytes,
675                                     m->m_pkthdr.len);
676                                 IF_ADDR_RUNLOCK(ifp);
677                                 goto ours;
678                         }
679 #ifdef BOOTP_COMPAT
680                         if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
681                                 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
682                                 counter_u64_add(ia->ia_ifa.ifa_ibytes,
683                                     m->m_pkthdr.len);
684                                 IF_ADDR_RUNLOCK(ifp);
685                                 goto ours;
686                         }
687 #endif
688                 }
689                 IF_ADDR_RUNLOCK(ifp);
690                 ia = NULL;
691         }
692         /* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
693         if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
694                 IPSTAT_INC(ips_cantforward);
695                 m_freem(m);
696                 return;
697         }
698         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
699                 if (V_ip_mrouter) {
700                         /*
701                          * If we are acting as a multicast router, all
702                          * incoming multicast packets are passed to the
703                          * kernel-level multicast forwarding function.
704                          * The packet is returned (relatively) intact; if
705                          * ip_mforward() returns a non-zero value, the packet
706                          * must be discarded, else it may be accepted below.
707                          */
708                         if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
709                                 IPSTAT_INC(ips_cantforward);
710                                 m_freem(m);
711                                 return;
712                         }
713
714                         /*
715                          * The process-level routing daemon needs to receive
716                          * all multicast IGMP packets, whether or not this
717                          * host belongs to their destination groups.
718                          */
719                         if (ip->ip_p == IPPROTO_IGMP)
720                                 goto ours;
721                         IPSTAT_INC(ips_forward);
722                 }
723                 /*
724                  * Assume the packet is for us, to avoid prematurely taking
725                  * a lock on the in_multi hash. Protocols must perform
726                  * their own filtering and update statistics accordingly.
727                  */
728                 goto ours;
729         }
730         if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
731                 goto ours;
732         if (ip->ip_dst.s_addr == INADDR_ANY)
733                 goto ours;
734
735         /*
736          * Not for us; forward if possible and desirable.
737          */
738         if (V_ipforwarding == 0) {
739                 IPSTAT_INC(ips_cantforward);
740                 m_freem(m);
741         } else {
742                 ip_forward(m, dchg);
743         }
744         return;
745
746 ours:
747 #ifdef IPSTEALTH
748         /*
749          * IPSTEALTH: Process non-routing options only
750          * if the packet is destined for us.
751          */
752         if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1))
753                 return;
754 #endif /* IPSTEALTH */
755
756         /*
757          * Attempt reassembly; if it succeeds, proceed.
758          * ip_reass() will return a different mbuf.
759          */
760         if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
761                 /* XXXGL: shouldn't we save & set m_flags? */
762                 m = ip_reass(m);
763                 if (m == NULL)
764                         return;
765                 ip = mtod(m, struct ip *);
766                 /* Get the header length of the reassembled packet */
767                 hlen = ip->ip_hl << 2;
768         }
769
770 #ifdef IPSEC
771         /*
772          * enforce IPsec policy checking if we are seeing last header.
773          * note that we do not visit this with protocols with pcb layer
774          * code - like udp/tcp/raw ip.
775          */
776         if (ip_ipsec_input(m, ip->ip_p) != 0)
777                 goto bad;
778 #endif /* IPSEC */
779
780         /*
781          * Switch out to protocol's input routine.
782          */
783         IPSTAT_INC(ips_delivered);
784
785         (*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p);
786         return;
787 bad:
788         m_freem(m);
789 }
790
791 /*
792  * IP timer processing;
793  * if a timer expires on a reassembly
794  * queue, discard it.
795  */
796 void
797 ip_slowtimo(void)
798 {
799         VNET_ITERATOR_DECL(vnet_iter);
800
801         VNET_LIST_RLOCK_NOSLEEP();
802         VNET_FOREACH(vnet_iter) {
803                 CURVNET_SET(vnet_iter);
804                 ipreass_slowtimo();
805                 CURVNET_RESTORE();
806         }
807         VNET_LIST_RUNLOCK_NOSLEEP();
808 }
809
810 void
811 ip_drain(void)
812 {
813         VNET_ITERATOR_DECL(vnet_iter);
814
815         VNET_LIST_RLOCK_NOSLEEP();
816         VNET_FOREACH(vnet_iter) {
817                 CURVNET_SET(vnet_iter);
818                 ipreass_drain();
819                 CURVNET_RESTORE();
820         }
821         VNET_LIST_RUNLOCK_NOSLEEP();
822 }
823
824 /*
825  * The protocol to be inserted into ip_protox[] must be already registered
826  * in inetsw[], either statically or through pf_proto_register().
827  */
828 int
829 ipproto_register(short ipproto)
830 {
831         struct protosw *pr;
832
833         /* Sanity checks. */
834         if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
835                 return (EPROTONOSUPPORT);
836
837         /*
838          * The protocol slot must not be occupied by another protocol
839          * already.  An index pointing to IPPROTO_RAW is unused.
840          */
841         pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
842         if (pr == NULL)
843                 return (EPFNOSUPPORT);
844         if (ip_protox[ipproto] != pr - inetsw)  /* IPPROTO_RAW */
845                 return (EEXIST);
846
847         /* Find the protocol position in inetsw[] and set the index. */
848         for (pr = inetdomain.dom_protosw;
849              pr < inetdomain.dom_protoswNPROTOSW; pr++) {
850                 if (pr->pr_domain->dom_family == PF_INET &&
851                     pr->pr_protocol && pr->pr_protocol == ipproto) {
852                         ip_protox[pr->pr_protocol] = pr - inetsw;
853                         return (0);
854                 }
855         }
856         return (EPROTONOSUPPORT);
857 }
858
859 int
860 ipproto_unregister(short ipproto)
861 {
862         struct protosw *pr;
863
864         /* Sanity checks. */
865         if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
866                 return (EPROTONOSUPPORT);
867
868         /* Check if the protocol was indeed registered. */
869         pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
870         if (pr == NULL)
871                 return (EPFNOSUPPORT);
872         if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
873                 return (ENOENT);
874
875         /* Reset the protocol slot to IPPROTO_RAW. */
876         ip_protox[ipproto] = pr - inetsw;
877         return (0);
878 }
879
880 u_char inetctlerrmap[PRC_NCMDS] = {
881         0,              0,              0,              0,
882         0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
883         EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
884         EMSGSIZE,       EHOSTUNREACH,   0,              0,
885         0,              0,              EHOSTUNREACH,   0,
886         ENOPROTOOPT,    ECONNREFUSED
887 };
888
889 /*
890  * Forward a packet.  If some error occurs return the sender
891  * an icmp packet.  Note we can't always generate a meaningful
892  * icmp message because icmp doesn't have a large enough repertoire
893  * of codes and types.
894  *
895  * If not forwarding, just drop the packet.  This could be confusing
896  * if ipforwarding was zero but some routing protocol was advancing
897  * us as a gateway to somewhere.  However, we must let the routing
898  * protocol deal with that.
899  *
900  * The srcrt parameter indicates whether the packet is being forwarded
901  * via a source route.
902  */
903 void
904 ip_forward(struct mbuf *m, int srcrt)
905 {
906         struct ip *ip = mtod(m, struct ip *);
907         struct in_ifaddr *ia;
908         struct mbuf *mcopy;
909         struct sockaddr_in *sin;
910         struct in_addr dest;
911         struct route ro;
912         int error, type = 0, code = 0, mtu = 0;
913
914         if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
915                 IPSTAT_INC(ips_cantforward);
916                 m_freem(m);
917                 return;
918         }
919 #ifdef IPSEC
920         if (ip_ipsec_fwd(m) != 0) {
921                 IPSTAT_INC(ips_cantforward);
922                 m_freem(m);
923                 return;
924         }
925 #endif /* IPSEC */
926 #ifdef IPSTEALTH
927         if (!V_ipstealth) {
928 #endif
929                 if (ip->ip_ttl <= IPTTLDEC) {
930                         icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
931                             0, 0);
932                         return;
933                 }
934 #ifdef IPSTEALTH
935         }
936 #endif
937
938         bzero(&ro, sizeof(ro));
939         sin = (struct sockaddr_in *)&ro.ro_dst;
940         sin->sin_family = AF_INET;
941         sin->sin_len = sizeof(*sin);
942         sin->sin_addr = ip->ip_dst;
943 #ifdef RADIX_MPATH
944         rtalloc_mpath_fib(&ro,
945             ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
946             M_GETFIB(m));
947 #else
948         in_rtalloc_ign(&ro, 0, M_GETFIB(m));
949 #endif
950         if (ro.ro_rt != NULL) {
951                 ia = ifatoia(ro.ro_rt->rt_ifa);
952                 ifa_ref(&ia->ia_ifa);
953         } else
954                 ia = NULL;
955 #ifndef IPSEC
956         /*
957          * 'ia' may be NULL if there is no route for this destination.
958          * In case of IPsec, Don't discard it just yet, but pass it to
959          * ip_output in case of outgoing IPsec policy.
960          */
961         if (!srcrt && ia == NULL) {
962                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
963                 RO_RTFREE(&ro);
964                 return;
965         }
966 #endif
967
968         /*
969          * Save the IP header and at most 8 bytes of the payload,
970          * in case we need to generate an ICMP message to the src.
971          *
972          * XXX this can be optimized a lot by saving the data in a local
973          * buffer on the stack (72 bytes at most), and only allocating the
974          * mbuf if really necessary. The vast majority of the packets
975          * are forwarded without having to send an ICMP back (either
976          * because unnecessary, or because rate limited), so we are
977          * really we are wasting a lot of work here.
978          *
979          * We don't use m_copy() because it might return a reference
980          * to a shared cluster. Both this function and ip_output()
981          * assume exclusive access to the IP header in `m', so any
982          * data in a cluster may change before we reach icmp_error().
983          */
984         mcopy = m_gethdr(M_NOWAIT, m->m_type);
985         if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
986                 /*
987                  * It's probably ok if the pkthdr dup fails (because
988                  * the deep copy of the tag chain failed), but for now
989                  * be conservative and just discard the copy since
990                  * code below may some day want the tags.
991                  */
992                 m_free(mcopy);
993                 mcopy = NULL;
994         }
995         if (mcopy != NULL) {
996                 mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy));
997                 mcopy->m_pkthdr.len = mcopy->m_len;
998                 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
999         }
1000
1001 #ifdef IPSTEALTH
1002         if (!V_ipstealth) {
1003 #endif
1004                 ip->ip_ttl -= IPTTLDEC;
1005 #ifdef IPSTEALTH
1006         }
1007 #endif
1008
1009         /*
1010          * If forwarding packet using same interface that it came in on,
1011          * perhaps should send a redirect to sender to shortcut a hop.
1012          * Only send redirect if source is sending directly to us,
1013          * and if packet was not source routed (or has any options).
1014          * Also, don't send redirect if forwarding using a default route
1015          * or a route modified by a redirect.
1016          */
1017         dest.s_addr = 0;
1018         if (!srcrt && V_ipsendredirects &&
1019             ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
1020                 struct rtentry *rt;
1021
1022                 rt = ro.ro_rt;
1023
1024                 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1025                     satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1026 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1027                         u_long src = ntohl(ip->ip_src.s_addr);
1028
1029                         if (RTA(rt) &&
1030                             (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1031                                 if (rt->rt_flags & RTF_GATEWAY)
1032                                         dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1033                                 else
1034                                         dest.s_addr = ip->ip_dst.s_addr;
1035                                 /* Router requirements says to only send host redirects */
1036                                 type = ICMP_REDIRECT;
1037                                 code = ICMP_REDIRECT_HOST;
1038                         }
1039                 }
1040         }
1041
1042         error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1043
1044         if (error == EMSGSIZE && ro.ro_rt)
1045                 mtu = ro.ro_rt->rt_mtu;
1046         RO_RTFREE(&ro);
1047
1048         if (error)
1049                 IPSTAT_INC(ips_cantforward);
1050         else {
1051                 IPSTAT_INC(ips_forward);
1052                 if (type)
1053                         IPSTAT_INC(ips_redirectsent);
1054                 else {
1055                         if (mcopy)
1056                                 m_freem(mcopy);
1057                         if (ia != NULL)
1058                                 ifa_free(&ia->ia_ifa);
1059                         return;
1060                 }
1061         }
1062         if (mcopy == NULL) {
1063                 if (ia != NULL)
1064                         ifa_free(&ia->ia_ifa);
1065                 return;
1066         }
1067
1068         switch (error) {
1069
1070         case 0:                         /* forwarded, but need redirect */
1071                 /* type, code set above */
1072                 break;
1073
1074         case ENETUNREACH:
1075         case EHOSTUNREACH:
1076         case ENETDOWN:
1077         case EHOSTDOWN:
1078         default:
1079                 type = ICMP_UNREACH;
1080                 code = ICMP_UNREACH_HOST;
1081                 break;
1082
1083         case EMSGSIZE:
1084                 type = ICMP_UNREACH;
1085                 code = ICMP_UNREACH_NEEDFRAG;
1086
1087 #ifdef IPSEC
1088                 /* 
1089                  * If IPsec is configured for this path,
1090                  * override any possibly mtu value set by ip_output.
1091                  */ 
1092                 mtu = ip_ipsec_mtu(mcopy, mtu);
1093 #endif /* IPSEC */
1094                 /*
1095                  * If the MTU was set before make sure we are below the
1096                  * interface MTU.
1097                  * If the MTU wasn't set before use the interface mtu or
1098                  * fall back to the next smaller mtu step compared to the
1099                  * current packet size.
1100                  */
1101                 if (mtu != 0) {
1102                         if (ia != NULL)
1103                                 mtu = min(mtu, ia->ia_ifp->if_mtu);
1104                 } else {
1105                         if (ia != NULL)
1106                                 mtu = ia->ia_ifp->if_mtu;
1107                         else
1108                                 mtu = ip_next_mtu(ntohs(ip->ip_len), 0);
1109                 }
1110                 IPSTAT_INC(ips_cantfrag);
1111                 break;
1112
1113         case ENOBUFS:
1114         case EACCES:                    /* ipfw denied packet */
1115                 m_freem(mcopy);
1116                 if (ia != NULL)
1117                         ifa_free(&ia->ia_ifa);
1118                 return;
1119         }
1120         if (ia != NULL)
1121                 ifa_free(&ia->ia_ifa);
1122         icmp_error(mcopy, type, code, dest.s_addr, mtu);
1123 }
1124
1125 void
1126 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1127     struct mbuf *m)
1128 {
1129
1130         if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1131                 struct bintime bt;
1132
1133                 bintime(&bt);
1134                 if (inp->inp_socket->so_options & SO_BINTIME) {
1135                         *mp = sbcreatecontrol((caddr_t)&bt, sizeof(bt),
1136                             SCM_BINTIME, SOL_SOCKET);
1137                         if (*mp)
1138                                 mp = &(*mp)->m_next;
1139                 }
1140                 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1141                         struct timeval tv;
1142
1143                         bintime2timeval(&bt, &tv);
1144                         *mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv),
1145                             SCM_TIMESTAMP, SOL_SOCKET);
1146                         if (*mp)
1147                                 mp = &(*mp)->m_next;
1148                 }
1149         }
1150         if (inp->inp_flags & INP_RECVDSTADDR) {
1151                 *mp = sbcreatecontrol((caddr_t)&ip->ip_dst,
1152                     sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1153                 if (*mp)
1154                         mp = &(*mp)->m_next;
1155         }
1156         if (inp->inp_flags & INP_RECVTTL) {
1157                 *mp = sbcreatecontrol((caddr_t)&ip->ip_ttl,
1158                     sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
1159                 if (*mp)
1160                         mp = &(*mp)->m_next;
1161         }
1162 #ifdef notyet
1163         /* XXX
1164          * Moving these out of udp_input() made them even more broken
1165          * than they already were.
1166          */
1167         /* options were tossed already */
1168         if (inp->inp_flags & INP_RECVOPTS) {
1169                 *mp = sbcreatecontrol((caddr_t)opts_deleted_above,
1170                     sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1171                 if (*mp)
1172                         mp = &(*mp)->m_next;
1173         }
1174         /* ip_srcroute doesn't do what we want here, need to fix */
1175         if (inp->inp_flags & INP_RECVRETOPTS) {
1176                 *mp = sbcreatecontrol((caddr_t)ip_srcroute(m),
1177                     sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1178                 if (*mp)
1179                         mp = &(*mp)->m_next;
1180         }
1181 #endif
1182         if (inp->inp_flags & INP_RECVIF) {
1183                 struct ifnet *ifp;
1184                 struct sdlbuf {
1185                         struct sockaddr_dl sdl;
1186                         u_char  pad[32];
1187                 } sdlbuf;
1188                 struct sockaddr_dl *sdp;
1189                 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1190
1191                 if ((ifp = m->m_pkthdr.rcvif) &&
1192                     ifp->if_index && ifp->if_index <= V_if_index) {
1193                         sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1194                         /*
1195                          * Change our mind and don't try copy.
1196                          */
1197                         if (sdp->sdl_family != AF_LINK ||
1198                             sdp->sdl_len > sizeof(sdlbuf)) {
1199                                 goto makedummy;
1200                         }
1201                         bcopy(sdp, sdl2, sdp->sdl_len);
1202                 } else {
1203 makedummy:      
1204                         sdl2->sdl_len =
1205                             offsetof(struct sockaddr_dl, sdl_data[0]);
1206                         sdl2->sdl_family = AF_LINK;
1207                         sdl2->sdl_index = 0;
1208                         sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1209                 }
1210                 *mp = sbcreatecontrol((caddr_t)sdl2, sdl2->sdl_len,
1211                     IP_RECVIF, IPPROTO_IP);
1212                 if (*mp)
1213                         mp = &(*mp)->m_next;
1214         }
1215         if (inp->inp_flags & INP_RECVTOS) {
1216                 *mp = sbcreatecontrol((caddr_t)&ip->ip_tos,
1217                     sizeof(u_char), IP_RECVTOS, IPPROTO_IP);
1218                 if (*mp)
1219                         mp = &(*mp)->m_next;
1220         }
1221
1222         if (inp->inp_flags2 & INP_RECVFLOWID) {
1223                 uint32_t flowid, flow_type;
1224
1225                 flowid = m->m_pkthdr.flowid;
1226                 flow_type = M_HASHTYPE_GET(m);
1227
1228                 /*
1229                  * XXX should handle the failure of one or the
1230                  * other - don't populate both?
1231                  */
1232                 *mp = sbcreatecontrol((caddr_t) &flowid,
1233                     sizeof(uint32_t), IP_FLOWID, IPPROTO_IP);
1234                 if (*mp)
1235                         mp = &(*mp)->m_next;
1236                 *mp = sbcreatecontrol((caddr_t) &flow_type,
1237                     sizeof(uint32_t), IP_FLOWTYPE, IPPROTO_IP);
1238                 if (*mp)
1239                         mp = &(*mp)->m_next;
1240         }
1241
1242 #ifdef  RSS
1243         if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
1244                 uint32_t flowid, flow_type;
1245                 uint32_t rss_bucketid;
1246
1247                 flowid = m->m_pkthdr.flowid;
1248                 flow_type = M_HASHTYPE_GET(m);
1249
1250                 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1251                         *mp = sbcreatecontrol((caddr_t) &rss_bucketid,
1252                            sizeof(uint32_t), IP_RSSBUCKETID, IPPROTO_IP);
1253                         if (*mp)
1254                                 mp = &(*mp)->m_next;
1255                 }
1256         }
1257 #endif
1258 }
1259
1260 /*
1261  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
1262  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
1263  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
1264  * compiled.
1265  */
1266 static VNET_DEFINE(int, ip_rsvp_on);
1267 VNET_DEFINE(struct socket *, ip_rsvpd);
1268
1269 #define V_ip_rsvp_on            VNET(ip_rsvp_on)
1270
1271 int
1272 ip_rsvp_init(struct socket *so)
1273 {
1274
1275         if (so->so_type != SOCK_RAW ||
1276             so->so_proto->pr_protocol != IPPROTO_RSVP)
1277                 return EOPNOTSUPP;
1278
1279         if (V_ip_rsvpd != NULL)
1280                 return EADDRINUSE;
1281
1282         V_ip_rsvpd = so;
1283         /*
1284          * This may seem silly, but we need to be sure we don't over-increment
1285          * the RSVP counter, in case something slips up.
1286          */
1287         if (!V_ip_rsvp_on) {
1288                 V_ip_rsvp_on = 1;
1289                 V_rsvp_on++;
1290         }
1291
1292         return 0;
1293 }
1294
1295 int
1296 ip_rsvp_done(void)
1297 {
1298
1299         V_ip_rsvpd = NULL;
1300         /*
1301          * This may seem silly, but we need to be sure we don't over-decrement
1302          * the RSVP counter, in case something slips up.
1303          */
1304         if (V_ip_rsvp_on) {
1305                 V_ip_rsvp_on = 0;
1306                 V_rsvp_on--;
1307         }
1308         return 0;
1309 }
1310
1311 int
1312 rsvp_input(struct mbuf **mp, int *offp, int proto)
1313 {
1314         struct mbuf *m;
1315
1316         m = *mp;
1317         *mp = NULL;
1318
1319         if (rsvp_input_p) { /* call the real one if loaded */
1320                 *mp = m;
1321                 rsvp_input_p(mp, offp, proto);
1322                 return (IPPROTO_DONE);
1323         }
1324
1325         /* Can still get packets with rsvp_on = 0 if there is a local member
1326          * of the group to which the RSVP packet is addressed.  But in this
1327          * case we want to throw the packet away.
1328          */
1329         
1330         if (!V_rsvp_on) {
1331                 m_freem(m);
1332                 return (IPPROTO_DONE);
1333         }
1334
1335         if (V_ip_rsvpd != NULL) { 
1336                 *mp = m;
1337                 rip_input(mp, offp, proto);
1338                 return (IPPROTO_DONE);
1339         }
1340         /* Drop the packet */
1341         m_freem(m);
1342         return (IPPROTO_DONE);
1343 }