]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/ip_input.c
Use TAILQ_FOREACH_SAFE() instead of implementing it ourselves.
[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/mbuf.h>
45 #include <sys/malloc.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/time.h>
50 #include <sys/kernel.h>
51 #include <sys/lock.h>
52 #include <sys/rwlock.h>
53 #include <sys/sdt.h>
54 #include <sys/syslog.h>
55 #include <sys/sysctl.h>
56
57 #include <net/pfil.h>
58 #include <net/if.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>
65 #include <net/vnet.h>
66
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>
79 #ifdef IPSEC
80 #include <netinet/ip_ipsec.h>
81 #endif /* IPSEC */
82 #include <netinet/in_rss.h>
83
84 #include <sys/socketvar.h>
85
86 #include <security/mac/mac_framework.h>
87
88 #ifdef CTASSERT
89 CTASSERT(sizeof(struct ip) == 20);
90 #endif
91
92 struct  rwlock in_ifaddr_lock;
93 RW_SYSINIT(in_ifaddr_lock, &in_ifaddr_lock, "in_ifaddr_lock");
94
95 VNET_DEFINE(int, rsvp_on);
96
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");
101
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");
107
108 /*
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.
113  *
114  * XXX - ip_checkinterface currently must be disabled if you use ipnat
115  * to translate the destination address to another local interface.
116  *
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.
120  */
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");
126
127 VNET_DEFINE(struct pfil_head, inet_pfil_hook);  /* Packet filter hooks */
128
129 static struct netisr_handler ip_nh = {
130         .nh_name = "ip",
131         .nh_handler = ip_input,
132         .nh_proto = NETISR_IP,
133 #ifdef  RSS
134         .nh_m2cpuid = rss_soft_m2cpuid,
135         .nh_policy = NETISR_POLICY_CPU,
136         .nh_dispatch = NETISR_DISPATCH_HYBRID,
137 #else
138         .nh_policy = NETISR_POLICY_FLOW,
139 #endif
140 };
141
142 #ifdef  RSS
143 /*
144  * Directly dispatched frames are currently assumed
145  * to have a flowid already calculated.
146  *
147  * It should likely have something that assert it
148  * actually has valid flow details.
149  */
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,
157 };
158 #endif
159
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 */
166
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];
170
171 #define V_ipq_zone              VNET(ipq_zone)
172 #define V_ipq                   VNET(ipq)
173
174 /*
175  * The ipqlock array is global, /not/ per-VNET.
176  */
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)
180
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 *);
185
186 static inline void
187 ipq_timeout(struct ipqhead *head, struct ipq *fp)
188 {
189
190         IPSTAT_ADD(ips_fragtimeout, fp->ipq_nfrags);
191         ipq_free(head, fp);
192 }
193
194 static inline void
195 ipq_drop(struct ipqhead *head, struct ipq *fp)
196 {
197
198         IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
199         ipq_free(head, fp);
200 }
201
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,
207     &VNET_NAME(nipq), 0,
208     "Current number of IPv4 fragment reassembly queue entries");
209
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");
215
216 #ifdef IPCTL_DEFMTU
217 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
218     &ip_mtu, 0, "Default MTU");
219 #endif
220
221 #ifdef IPSTEALTH
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");
226 #endif
227
228 /*
229  * IP statistics are stored in the "array" of counter(9)s.
230  */
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)");
235
236 #ifdef VIMAGE
237 VNET_PCPUSTAT_SYSUNINIT(ipstat);
238 #endif /* VIMAGE */
239
240 /*
241  * Kernel module interface for updating ipstat.  The argument is an index
242  * into ipstat treated as an array.
243  */
244 void
245 kmod_ipstat_inc(int statnum)
246 {
247
248         counter_u64_add(VNET(ipstat)[statnum], 1);
249 }
250
251 void
252 kmod_ipstat_dec(int statnum)
253 {
254
255         counter_u64_add(VNET(ipstat)[statnum], -1);
256 }
257
258 static int
259 sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
260 {
261         int error, qlimit;
262
263         netisr_getqlimit(&ip_nh, &qlimit);
264         error = sysctl_handle_int(oidp, &qlimit, 0, req);
265         if (error || !req->newptr)
266                 return (error);
267         if (qlimit < 1)
268                 return (EINVAL);
269         return (netisr_setqlimit(&ip_nh, qlimit));
270 }
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");
274
275 static int
276 sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
277 {
278         u_int64_t qdrops_long;
279         int error, qdrops;
280
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)
285                 return (error);
286         if (qdrops != 0)
287                 return (EINVAL);
288         netisr_clearqdrops(&ip_nh);
289         return (0);
290 }
291
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");
295
296 #ifdef  RSS
297 static int
298 sysctl_netinet_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
299 {
300         int error, qlimit;
301
302         netisr_getqlimit(&ip_direct_nh, &qlimit);
303         error = sysctl_handle_int(oidp, &qlimit, 0, req);
304         if (error || !req->newptr)
305                 return (error);
306         if (qlimit < 1)
307                 return (EINVAL);
308         return (netisr_setqlimit(&ip_direct_nh, qlimit));
309 }
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");
313
314 static int
315 sysctl_netinet_intr_direct_queue_drops(SYSCTL_HANDLER_ARGS)
316 {
317         u_int64_t qdrops_long;
318         int error, qdrops;
319
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)
324                 return (error);
325         if (qdrops != 0)
326                 return (EINVAL);
327         netisr_clearqdrops(&ip_direct_nh);
328         return (0);
329 }
330
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");
334 #endif  /* RSS */
335
336 /*
337  * IP initialization: fill in IP protocol switch table.
338  * All protocols not implemented in kernel go to raw IP protocol handler.
339  */
340 void
341 ip_init(void)
342 {
343         struct protosw *pr;
344         int i;
345
346         TAILQ_INIT(&V_in_ifaddrhead);
347         V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
348
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);
356         maxnipq_update();
357
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);
364
365         /* Skip initialization of globals for non-default instances. */
366         if (!IS_DEFAULT_VNET(curvnet))
367                 return;
368
369         pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
370         if (pr == NULL)
371                 panic("ip_init: PF_INET not found");
372
373         /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
374         for (i = 0; i < IPPROTO_MAX; i++)
375                 ip_protox[i] = pr - inetsw;
376         /*
377          * Cycle through IP protocols and put them into the appropriate place
378          * in ip_protox[].
379          */
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;
387                 }
388
389         EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
390                 NULL, EVENTHANDLER_PRI_ANY);
391
392         /* Initialize various other remaining things. */
393         for (i = 0; i < IPREASS_NHASH; i++)
394                 IPQ_LOCK_INIT(i);
395         netisr_register(&ip_nh);
396 #ifdef  RSS
397         netisr_register(&ip_direct_nh);
398 #endif
399 }
400
401 #ifdef VIMAGE
402 void
403 ip_destroy(void)
404 {
405         int i;
406
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);
410
411         /* Cleanup in_ifaddr hash table; should be empty. */
412         hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask);
413
414         ip_drain_vnet();
415
416         uma_zdestroy(V_ipq_zone);
417 }
418 #endif
419
420 #ifdef  RSS
421 /*
422  * IP direct input routine.
423  *
424  * This is called when reinjecting completed fragments where
425  * all of the previous checking and book-keeping has been done.
426  */
427 void
428 ip_direct_input(struct mbuf *m)
429 {
430         struct ip *ip;
431         int hlen;
432
433         ip = mtod(m, struct ip *);
434         hlen = ip->ip_hl << 2;
435
436         IPSTAT_INC(ips_delivered);
437         (*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p);
438         return;
439 }
440 #endif
441
442 /*
443  * Ip input routine.  Checksum and byte swap header.  If fragmented
444  * try to reassemble.  Process options.  Pass to next level.
445  */
446 void
447 ip_input(struct mbuf *m)
448 {
449         struct ip *ip = NULL;
450         struct in_ifaddr *ia = NULL;
451         struct ifaddr *ifa;
452         struct ifnet *ifp;
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 */
457
458         M_ASSERTPKTHDR(m);
459
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);
466                 goto ours;
467         }
468
469         IPSTAT_INC(ips_total);
470
471         if (m->m_pkthdr.len < sizeof(struct ip))
472                 goto tooshort;
473
474         if (m->m_len < sizeof (struct ip) &&
475             (m = m_pullup(m, sizeof (struct ip))) == NULL) {
476                 IPSTAT_INC(ips_toosmall);
477                 return;
478         }
479         ip = mtod(m, struct ip *);
480
481         if (ip->ip_v != IPVERSION) {
482                 IPSTAT_INC(ips_badvers);
483                 goto bad;
484         }
485
486         hlen = ip->ip_hl << 2;
487         if (hlen < sizeof(struct ip)) { /* minimum header length */
488                 IPSTAT_INC(ips_badhlen);
489                 goto bad;
490         }
491         if (hlen > m->m_len) {
492                 if ((m = m_pullup(m, hlen)) == NULL) {
493                         IPSTAT_INC(ips_badhlen);
494                         return;
495                 }
496                 ip = mtod(m, struct ip *);
497         }
498
499         IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL);
500
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);
507                         goto bad;
508                 }
509         }
510
511         if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
512                 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
513         } else {
514                 if (hlen == sizeof(struct ip)) {
515                         sum = in_cksum_hdr(ip);
516                 } else {
517                         sum = in_cksum(m, hlen);
518                 }
519         }
520         if (sum) {
521                 IPSTAT_INC(ips_badsum);
522                 goto bad;
523         }
524
525 #ifdef ALTQ
526         if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
527                 /* packet is dropped by traffic conditioner */
528                 return;
529 #endif
530
531         ip_len = ntohs(ip->ip_len);
532         if (ip_len < hlen) {
533                 IPSTAT_INC(ips_badlen);
534                 goto bad;
535         }
536
537         /*
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.
542          */
543         if (m->m_pkthdr.len < ip_len) {
544 tooshort:
545                 IPSTAT_INC(ips_tooshort);
546                 goto bad;
547         }
548         if (m->m_pkthdr.len > ip_len) {
549                 if (m->m_len == m->m_pkthdr.len) {
550                         m->m_len = ip_len;
551                         m->m_pkthdr.len = ip_len;
552                 } else
553                         m_adj(m, ip_len - m->m_pkthdr.len);
554         }
555
556 #ifdef IPSEC
557         /*
558          * Bypass packet filtering for packets previously handled by IPsec.
559          */
560         if (ip_ipsec_filtertunnel(m))
561                 goto passin;
562 #endif /* IPSEC */
563
564         /*
565          * Run through list of hooks for input packets.
566          *
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.
570          */
571
572         /* Jump over all PFIL processing if hooks are not active. */
573         if (!PFIL_HOOKED(&V_inet_pfil_hook))
574                 goto passin;
575
576         odst = ip->ip_dst;
577         if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0)
578                 return;
579         if (m == NULL)                  /* consumed by filter */
580                 return;
581
582         ip = mtod(m, struct ip *);
583         dchg = (odst.s_addr != ip->ip_dst.s_addr);
584         ifp = m->m_pkthdr.rcvif;
585
586         if (m->m_flags & M_FASTFWD_OURS) {
587                 m->m_flags &= ~M_FASTFWD_OURS;
588                 goto ours;
589         }
590         if (m->m_flags & M_IP_NEXTHOP) {
591                 dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL);
592                 if (dchg != 0) {
593                         /*
594                          * Directly ship the packet on.  This allows
595                          * forwarding packets originally destined to us
596                          * to some other directly connected host.
597                          */
598                         ip_forward(m, 1);
599                         return;
600                 }
601         }
602 passin:
603
604         /*
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).
609          */
610         if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
611                 return;
612
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.
618          */
619         if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP) 
620                 goto ours;
621
622         /*
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
626          * with it).
627          */
628         if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
629             (m->m_flags & (M_MCAST|M_BCAST)) == 0)
630                 goto ours;
631
632         /*
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
637          * 'ipfw fwd'.
638          *
639          * XXX - Checking also should be disabled if the destination
640          * address is ipnat'ed to a different interface.
641          *
642          * XXX - Checking is incompatible with IP aliases added
643          * to the loopback interface instead of the interface where
644          * the packets are received.
645          *
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().
649          */
650         checkif = V_ip_checkinterface && (V_ipforwarding == 0) && 
651             ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
652             ifp->if_carp == NULL && (dchg == 0);
653
654         /*
655          * Check for exact addresses in the hash bucket.
656          */
657         /* IN_IFADDR_RLOCK(); */
658         LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
659                 /*
660                  * If the address matches, verify that the packet
661                  * arrived via the correct interface if checking is
662                  * enabled.
663                  */
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,
668                             m->m_pkthdr.len);
669                         /* IN_IFADDR_RUNLOCK(); */
670                         goto ours;
671                 }
672         }
673         /* IN_IFADDR_RUNLOCK(); */
674
675         /*
676          * Check for broadcast addresses.
677          *
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().
682          */
683         if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
684                 IF_ADDR_RLOCK(ifp);
685                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
686                         if (ifa->ifa_addr->sa_family != AF_INET)
687                                 continue;
688                         ia = ifatoia(ifa);
689                         if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
690                             ip->ip_dst.s_addr) {
691                                 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
692                                 counter_u64_add(ia->ia_ifa.ifa_ibytes,
693                                     m->m_pkthdr.len);
694                                 IF_ADDR_RUNLOCK(ifp);
695                                 goto ours;
696                         }
697 #ifdef BOOTP_COMPAT
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,
701                                     m->m_pkthdr.len);
702                                 IF_ADDR_RUNLOCK(ifp);
703                                 goto ours;
704                         }
705 #endif
706                 }
707                 IF_ADDR_RUNLOCK(ifp);
708                 ia = NULL;
709         }
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);
713                 m_freem(m);
714                 return;
715         }
716         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
717                 if (V_ip_mrouter) {
718                         /*
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.
725                          */
726                         if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
727                                 IPSTAT_INC(ips_cantforward);
728                                 m_freem(m);
729                                 return;
730                         }
731
732                         /*
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.
736                          */
737                         if (ip->ip_p == IPPROTO_IGMP)
738                                 goto ours;
739                         IPSTAT_INC(ips_forward);
740                 }
741                 /*
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.
745                  */
746                 goto ours;
747         }
748         if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
749                 goto ours;
750         if (ip->ip_dst.s_addr == INADDR_ANY)
751                 goto ours;
752
753         /*
754          * Not for us; forward if possible and desirable.
755          */
756         if (V_ipforwarding == 0) {
757                 IPSTAT_INC(ips_cantforward);
758                 m_freem(m);
759         } else {
760                 ip_forward(m, dchg);
761         }
762         return;
763
764 ours:
765 #ifdef IPSTEALTH
766         /*
767          * IPSTEALTH: Process non-routing options only
768          * if the packet is destined for us.
769          */
770         if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1))
771                 return;
772 #endif /* IPSTEALTH */
773
774         /*
775          * Attempt reassembly; if it succeeds, proceed.
776          * ip_reass() will return a different mbuf.
777          */
778         if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
779                 /* XXXGL: shouldn't we save & set m_flags? */
780                 m = ip_reass(m);
781                 if (m == NULL)
782                         return;
783                 ip = mtod(m, struct ip *);
784                 /* Get the header length of the reassembled packet */
785                 hlen = ip->ip_hl << 2;
786         }
787
788 #ifdef IPSEC
789         /*
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.
793          */
794         if (ip_ipsec_input(m, ip->ip_p) != 0)
795                 goto bad;
796 #endif /* IPSEC */
797
798         /*
799          * Switch out to protocol's input routine.
800          */
801         IPSTAT_INC(ips_delivered);
802
803         (*inetsw[ip_protox[ip->ip_p]].pr_input)(&m, &hlen, ip->ip_p);
804         return;
805 bad:
806         m_freem(m);
807 }
808
809 /*
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
812  * reasons.
813  */
814 static void
815 maxnipq_update(void)
816 {
817
818         /*
819          * -1 for unlimited allocation.
820          */
821         if (V_maxnipq < 0)
822                 uma_zone_set_max(V_ipq_zone, 0);
823         /*
824          * Positive number for specific bound.
825          */
826         if (V_maxnipq > 0)
827                 uma_zone_set_max(V_ipq_zone, V_maxnipq);
828         /*
829          * Zero specifies no further fragment queue allocation.
830          */
831         if (V_maxnipq == 0) {
832                 uma_zone_set_max(V_ipq_zone, 1);
833                 ip_drain_vnet();
834         }
835 }
836
837 static void
838 ipq_zone_change(void *tag)
839 {
840
841         if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
842                 V_maxnipq = nmbclusters / 32;
843                 maxnipq_update();
844         }
845 }
846
847 static int
848 sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
849 {
850         int error, i;
851
852         i = V_maxnipq;
853         error = sysctl_handle_int(oidp, &i, 0, req);
854         if (error || !req->newptr)
855                 return (error);
856
857         /*
858          * XXXRW: Might be a good idea to sanity check the argument and place
859          * an extreme upper bound.
860          */
861         if (i < -1)
862                 return (EINVAL);
863         V_maxnipq = i;
864         maxnipq_update();
865         return (0);
866 }
867
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");
871
872 #define M_IP_FRAG       M_PROTO9
873
874 /*
875  * Attempt to purge something from the reassembly queue to make
876  * room.
877  *
878  * Must be called without any IPQ locks held, as it will attempt
879  * to lock each in turn.
880  *
881  * 'skip_bucket' is the bucket with which to skip over, or -1 to
882  * not skip over anything.
883  *
884  * Returns the bucket being freed, or -1 for no action.
885  */
886 static int
887 ip_reass_purge_element(int skip_bucket)
888 {
889         int i;
890         struct ipq *r;
891
892         for (i = 0; i < IPREASS_NHASH; i++) {
893                 if (skip_bucket > -1 && i == skip_bucket)
894                         continue;
895                 IPQ_LOCK(i);
896                 r = TAILQ_LAST(&V_ipq[i], ipqhead);
897                 if (r) {
898                         ipq_timeout(&V_ipq[i], r);
899                         IPQ_UNLOCK(i);
900                         return (i);
901                 }
902                 IPQ_UNLOCK(i);
903         }
904         return (-1);
905 }
906
907 /*
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.
916  */
917 struct mbuf *
918 ip_reass(struct mbuf *m)
919 {
920         struct ip *ip;
921         struct mbuf *p, *q, *nq, *t;
922         struct ipq *fp = NULL;
923         struct ipqhead *head;
924         int i, hlen, next;
925         u_int8_t ecn, ecn0;
926         u_short hash;
927 #ifdef  RSS
928         uint32_t rss_hash, rss_type;
929 #endif
930         int do_purge = 0;
931
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);
936                 m_freem(m);
937                 return (NULL);
938         }
939
940         ip = mtod(m, struct ip *);
941         hlen = ip->ip_hl << 2;
942
943         hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
944         head = &V_ipq[hash];
945         IPQ_LOCK(hash);
946
947         /*
948          * Look for queue of fragments
949          * of this datagram.
950          */
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 &&
955 #ifdef MAC
956                     mac_ipq_match(m, fp) &&
957 #endif
958                     ip->ip_p == fp->ipq_p)
959                         goto found;
960
961         fp = NULL;
962
963         /*
964          * Attempt to trim the number of allocated fragment queues if it
965          * exceeds the administrative limit.
966          */
967         if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
968                 /*
969                  * drop something from the tail of the current queue
970                  * before proceeding further
971                  */
972                 struct ipq *q = TAILQ_LAST(head, ipqhead);
973                 if (q == NULL) {   /* gak */
974                         /*
975                          * Defer doing this until later; when the
976                          * lock is no longer held.
977                          */
978                         do_purge = 1;
979                 } else
980                         ipq_timeout(head, q);
981         }
982
983 found:
984         /*
985          * Adjust ip_len to not reflect header,
986          * convert offset of this to bytes.
987          */
988         ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
989         if (ip->ip_off & htons(IP_MF)) {
990                 /*
991                  * Make sure that fragments have a data length
992                  * that's a non-zero multiple of 8 bytes.
993                  */
994                 if (ip->ip_len == htons(0) || (ntohs(ip->ip_len) & 0x7) != 0) {
995                         IPSTAT_INC(ips_toosmall); /* XXX */
996                         goto dropfrag;
997                 }
998                 m->m_flags |= M_IP_FRAG;
999         } else
1000                 m->m_flags &= ~M_IP_FRAG;
1001         ip->ip_off = htons(ntohs(ip->ip_off) << 3);
1002
1003         /*
1004          * Attempt reassembly; if it succeeds, proceed.
1005          * ip_reass() will return a different mbuf.
1006          */
1007         IPSTAT_INC(ips_fragments);
1008         m->m_pkthdr.PH_loc.ptr = ip;
1009
1010         /* Previous ip_reass() started here. */
1011         /*
1012          * Presence of header sizes in mbufs
1013          * would confuse code below.
1014          */
1015         m->m_data += hlen;
1016         m->m_len -= hlen;
1017
1018         /*
1019          * If first fragment to arrive, create a reassembly queue.
1020          */
1021         if (fp == NULL) {
1022                 fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
1023                 if (fp == NULL)
1024                         goto dropfrag;
1025 #ifdef MAC
1026                 if (mac_ipq_init(fp, M_NOWAIT) != 0) {
1027                         uma_zfree(V_ipq_zone, fp);
1028                         fp = NULL;
1029                         goto dropfrag;
1030                 }
1031                 mac_ipq_create(m, fp);
1032 #endif
1033                 TAILQ_INSERT_HEAD(head, fp, ipq_list);
1034                 V_nipq++;
1035                 fp->ipq_nfrags = 1;
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;
1041                 fp->ipq_frags = m;
1042                 m->m_nextpkt = NULL;
1043                 goto done;
1044         } else {
1045                 fp->ipq_nfrags++;
1046 #ifdef MAC
1047                 mac_ipq_update(m, fp);
1048 #endif
1049         }
1050
1051 #define GETIP(m)        ((struct ip*)((m)->m_pkthdr.PH_loc.ptr))
1052
1053         /*
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.
1057          */
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)
1062                         goto dropfrag;
1063                 if (ecn0 != IPTOS_ECN_CE)
1064                         GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
1065         }
1066         if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
1067                 goto dropfrag;
1068
1069         /*
1070          * Find a segment which begins after this one does.
1071          */
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))
1074                         break;
1075
1076         /*
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.
1081          *
1082          * If some of the data is dropped from the preceding
1083          * segment, then it's checksum is invalidated.
1084          */
1085         if (p) {
1086                 i = ntohs(GETIP(p)->ip_off) + ntohs(GETIP(p)->ip_len) -
1087                     ntohs(ip->ip_off);
1088                 if (i > 0) {
1089                         if (i >= ntohs(ip->ip_len))
1090                                 goto dropfrag;
1091                         m_adj(m, i);
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);
1095                 }
1096                 m->m_nextpkt = p->m_nextpkt;
1097                 p->m_nextpkt = m;
1098         } else {
1099                 m->m_nextpkt = fp->ipq_frags;
1100                 fp->ipq_frags = m;
1101         }
1102
1103         /*
1104          * While we overlap succeeding segments trim them or,
1105          * if they are completely covered, dequeue them.
1106          */
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);
1114                         m_adj(q, i);
1115                         q->m_pkthdr.csum_flags = 0;
1116                         break;
1117                 }
1118                 nq = q->m_nextpkt;
1119                 m->m_nextpkt = nq;
1120                 IPSTAT_INC(ips_fragdropped);
1121                 fp->ipq_nfrags--;
1122                 m_freem(q);
1123         }
1124
1125         /*
1126          * Check for complete reassembly and perform frag per packet
1127          * limiting.
1128          *
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.)
1133          *
1134          */
1135         next = 0;
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)
1139                                 ipq_drop(head, fp);
1140                         goto done;
1141                 }
1142                 next += ntohs(GETIP(q)->ip_len);
1143         }
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)
1147                         ipq_drop(head, fp);
1148                 goto done;
1149         }
1150
1151         /*
1152          * Reassembly is complete.  Make sure the packet is a sane size.
1153          */
1154         q = fp->ipq_frags;
1155         ip = GETIP(q);
1156         if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
1157                 IPSTAT_INC(ips_toolong);
1158                 ipq_drop(head, fp);
1159                 goto done;
1160         }
1161
1162         /*
1163          * Concatenate fragments.
1164          */
1165         m = q;
1166         t = m->m_next;
1167         m->m_next = NULL;
1168         m_cat(m, t);
1169         nq = q->m_nextpkt;
1170         q->m_nextpkt = NULL;
1171         for (q = nq; q != NULL; q = nq) {
1172                 nq = q->m_nextpkt;
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;
1176                 m_cat(m, q);
1177         }
1178         /*
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.
1182          */
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);
1186 #ifdef MAC
1187         mac_ipq_reassemble(fp, m);
1188         mac_ipq_destroy(fp);
1189 #endif
1190
1191         /*
1192          * Create header for new ip packet by modifying header of first
1193          * packet;  dequeue and discard fragment reassembly header.
1194          * Make header visible.
1195          */
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);
1200         V_nipq--;
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 */
1206                 m_fixhdr(m);
1207         IPSTAT_INC(ips_reassembled);
1208         IPQ_UNLOCK(hash);
1209
1210         /*
1211          * Do the delayed purge to keep fragment counts under
1212          * the configured maximum.
1213          *
1214          * This is delayed so that it's not done with another IPQ bucket
1215          * lock held.
1216          *
1217          * Note that we pass in the bucket to /skip/ over, not
1218          * the bucket to /purge/.
1219          */
1220         if (do_purge)
1221                 ip_reass_purge_element(hash);
1222
1223 #ifdef  RSS
1224         /*
1225          * Query the RSS layer for the flowid / flowtype for the
1226          * mbuf payload.
1227          *
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.
1231          *
1232          * We then queue into the relevant netisr so it can be dispatched
1233          * to the correct CPU.
1234          *
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.
1237          */
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);
1241         }
1242
1243         /*
1244          * Queue/dispatch for reprocessing.
1245          *
1246          * Note: this is much slower than just handling the frame in the
1247          * current receive context.  It's likely worth investigating
1248          * why this is.
1249          */
1250         netisr_dispatch(NETISR_IP_DIRECT, m);
1251         return (NULL);
1252 #endif
1253
1254         /* Handle in-line */
1255         return (m);
1256
1257 dropfrag:
1258         IPSTAT_INC(ips_fragdropped);
1259         if (fp != NULL)
1260                 fp->ipq_nfrags--;
1261         m_freem(m);
1262 done:
1263         IPQ_UNLOCK(hash);
1264         return (NULL);
1265
1266 #undef GETIP
1267 }
1268
1269 /*
1270  * Free a fragment reassembly header and all
1271  * associated datagrams.
1272  */
1273 static void
1274 ipq_free(struct ipqhead *fhp, struct ipq *fp)
1275 {
1276         struct mbuf *q;
1277
1278         while (fp->ipq_frags) {
1279                 q = fp->ipq_frags;
1280                 fp->ipq_frags = q->m_nextpkt;
1281                 m_freem(q);
1282         }
1283         TAILQ_REMOVE(fhp, fp, ipq_list);
1284         uma_zfree(V_ipq_zone, fp);
1285         V_nipq--;
1286 }
1287
1288 /*
1289  * IP timer processing;
1290  * if a timer expires on a reassembly
1291  * queue, discard it.
1292  */
1293 void
1294 ip_slowtimo(void)
1295 {
1296         VNET_ITERATOR_DECL(vnet_iter);
1297         struct ipq *fp, *tmp;
1298         int i;
1299
1300         VNET_LIST_RLOCK_NOSLEEP();
1301         VNET_FOREACH(vnet_iter) {
1302                 CURVNET_SET(vnet_iter);
1303                 for (i = 0; i < IPREASS_NHASH; i++) {
1304                         IPQ_LOCK(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);
1308                         IPQ_UNLOCK(i);
1309                 }
1310                 /*
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.
1314                  */
1315                 if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1316                         for (i = 0; i < IPREASS_NHASH; i++) {
1317                                 IPQ_LOCK(i);
1318                                 while (V_nipq > V_maxnipq &&
1319                                     !TAILQ_EMPTY(&V_ipq[i]))
1320                                         ipq_drop(&V_ipq[i],
1321                                             TAILQ_FIRST(&V_ipq[i]));
1322                                 IPQ_UNLOCK(i);
1323                         }
1324                 }
1325                 CURVNET_RESTORE();
1326         }
1327         VNET_LIST_RUNLOCK_NOSLEEP();
1328 }
1329
1330 /*
1331  * Drain off all datagram fragments.
1332  */
1333 static void
1334 ip_drain_vnet(void)
1335 {
1336         int     i;
1337
1338         for (i = 0; i < IPREASS_NHASH; i++) {
1339                 IPQ_LOCK(i);
1340                 while(!TAILQ_EMPTY(&V_ipq[i]))
1341                         ipq_drop(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1342                 IPQ_UNLOCK(i);
1343         }
1344 }
1345
1346 void
1347 ip_drain(void)
1348 {
1349         VNET_ITERATOR_DECL(vnet_iter);
1350
1351         VNET_LIST_RLOCK_NOSLEEP();
1352         VNET_FOREACH(vnet_iter) {
1353                 CURVNET_SET(vnet_iter);
1354                 ip_drain_vnet();
1355                 CURVNET_RESTORE();
1356         }
1357         VNET_LIST_RUNLOCK_NOSLEEP();
1358 }
1359
1360 /*
1361  * The protocol to be inserted into ip_protox[] must be already registered
1362  * in inetsw[], either statically or through pf_proto_register().
1363  */
1364 int
1365 ipproto_register(short ipproto)
1366 {
1367         struct protosw *pr;
1368
1369         /* Sanity checks. */
1370         if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1371                 return (EPROTONOSUPPORT);
1372
1373         /*
1374          * The protocol slot must not be occupied by another protocol
1375          * already.  An index pointing to IPPROTO_RAW is unused.
1376          */
1377         pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1378         if (pr == NULL)
1379                 return (EPFNOSUPPORT);
1380         if (ip_protox[ipproto] != pr - inetsw)  /* IPPROTO_RAW */
1381                 return (EEXIST);
1382
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;
1389                         return (0);
1390                 }
1391         }
1392         return (EPROTONOSUPPORT);
1393 }
1394
1395 int
1396 ipproto_unregister(short ipproto)
1397 {
1398         struct protosw *pr;
1399
1400         /* Sanity checks. */
1401         if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1402                 return (EPROTONOSUPPORT);
1403
1404         /* Check if the protocol was indeed registered. */
1405         pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1406         if (pr == NULL)
1407                 return (EPFNOSUPPORT);
1408         if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1409                 return (ENOENT);
1410
1411         /* Reset the protocol slot to IPPROTO_RAW. */
1412         ip_protox[ipproto] = pr - inetsw;
1413         return (0);
1414 }
1415
1416 /*
1417  * Given address of next destination (final or next hop), return (referenced)
1418  * internet address info of interface to be used to get there.
1419  */
1420 struct in_ifaddr *
1421 ip_rtaddr(struct in_addr dst, u_int fibnum)
1422 {
1423         struct route sro;
1424         struct sockaddr_in *sin;
1425         struct in_ifaddr *ia;
1426
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);
1433
1434         if (sro.ro_rt == NULL)
1435                 return (NULL);
1436
1437         ia = ifatoia(sro.ro_rt->rt_ifa);
1438         ifa_ref(&ia->ia_ifa);
1439         RTFREE(sro.ro_rt);
1440         return (ia);
1441 }
1442
1443 u_char inetctlerrmap[PRC_NCMDS] = {
1444         0,              0,              0,              0,
1445         0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
1446         EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
1447         EMSGSIZE,       EHOSTUNREACH,   0,              0,
1448         0,              0,              EHOSTUNREACH,   0,
1449         ENOPROTOOPT,    ECONNREFUSED
1450 };
1451
1452 /*
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.
1457  *
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.
1462  *
1463  * The srcrt parameter indicates whether the packet is being forwarded
1464  * via a source route.
1465  */
1466 void
1467 ip_forward(struct mbuf *m, int srcrt)
1468 {
1469         struct ip *ip = mtod(m, struct ip *);
1470         struct in_ifaddr *ia;
1471         struct mbuf *mcopy;
1472         struct in_addr dest;
1473         struct route ro;
1474         int error, type = 0, code = 0, mtu = 0;
1475
1476         if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1477                 IPSTAT_INC(ips_cantforward);
1478                 m_freem(m);
1479                 return;
1480         }
1481 #ifdef IPSEC
1482         if (ip_ipsec_fwd(m) != 0) {
1483                 IPSTAT_INC(ips_cantforward);
1484                 m_freem(m);
1485                 return;
1486         }
1487 #endif /* IPSEC */
1488 #ifdef IPSTEALTH
1489         if (!V_ipstealth) {
1490 #endif
1491                 if (ip->ip_ttl <= IPTTLDEC) {
1492                         icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1493                             0, 0);
1494                         return;
1495                 }
1496 #ifdef IPSTEALTH
1497         }
1498 #endif
1499
1500         ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1501 #ifndef IPSEC
1502         /*
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.
1506          */
1507         if (!srcrt && ia == NULL) {
1508                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1509                 return;
1510         }
1511 #endif
1512
1513         /*
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.
1516          *
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.
1523          *
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().
1528          */
1529         mcopy = m_gethdr(M_NOWAIT, m->m_type);
1530         if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
1531                 /*
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.
1536                  */
1537                 m_free(mcopy);
1538                 mcopy = NULL;
1539         }
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));
1544         }
1545
1546 #ifdef IPSTEALTH
1547         if (!V_ipstealth) {
1548 #endif
1549                 ip->ip_ttl -= IPTTLDEC;
1550 #ifdef IPSTEALTH
1551         }
1552 #endif
1553
1554         /*
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.
1561          */
1562         dest.s_addr = 0;
1563         if (!srcrt && V_ipsendredirects &&
1564             ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
1565                 struct sockaddr_in *sin;
1566                 struct rtentry *rt;
1567
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));
1574
1575                 rt = ro.ro_rt;
1576
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);
1581
1582                         if (RTA(rt) &&
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;
1586                                 else
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;
1591                         }
1592                 }
1593                 if (rt)
1594                         RTFREE(rt);
1595         }
1596
1597         /*
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.
1600          */
1601         bzero(&ro, sizeof(ro));
1602
1603         error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1604
1605         if (error == EMSGSIZE && ro.ro_rt)
1606                 mtu = ro.ro_rt->rt_mtu;
1607         RO_RTFREE(&ro);
1608
1609         if (error)
1610                 IPSTAT_INC(ips_cantforward);
1611         else {
1612                 IPSTAT_INC(ips_forward);
1613                 if (type)
1614                         IPSTAT_INC(ips_redirectsent);
1615                 else {
1616                         if (mcopy)
1617                                 m_freem(mcopy);
1618                         if (ia != NULL)
1619                                 ifa_free(&ia->ia_ifa);
1620                         return;
1621                 }
1622         }
1623         if (mcopy == NULL) {
1624                 if (ia != NULL)
1625                         ifa_free(&ia->ia_ifa);
1626                 return;
1627         }
1628
1629         switch (error) {
1630
1631         case 0:                         /* forwarded, but need redirect */
1632                 /* type, code set above */
1633                 break;
1634
1635         case ENETUNREACH:
1636         case EHOSTUNREACH:
1637         case ENETDOWN:
1638         case EHOSTDOWN:
1639         default:
1640                 type = ICMP_UNREACH;
1641                 code = ICMP_UNREACH_HOST;
1642                 break;
1643
1644         case EMSGSIZE:
1645                 type = ICMP_UNREACH;
1646                 code = ICMP_UNREACH_NEEDFRAG;
1647
1648 #ifdef IPSEC
1649                 /* 
1650                  * If IPsec is configured for this path,
1651                  * override any possibly mtu value set by ip_output.
1652                  */ 
1653                 mtu = ip_ipsec_mtu(mcopy, mtu);
1654 #endif /* IPSEC */
1655                 /*
1656                  * If the MTU was set before make sure we are below the
1657                  * interface MTU.
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.
1661                  */
1662                 if (mtu != 0) {
1663                         if (ia != NULL)
1664                                 mtu = min(mtu, ia->ia_ifp->if_mtu);
1665                 } else {
1666                         if (ia != NULL)
1667                                 mtu = ia->ia_ifp->if_mtu;
1668                         else
1669                                 mtu = ip_next_mtu(ntohs(ip->ip_len), 0);
1670                 }
1671                 IPSTAT_INC(ips_cantfrag);
1672                 break;
1673
1674         case ENOBUFS:
1675         case EACCES:                    /* ipfw denied packet */
1676                 m_freem(mcopy);
1677                 if (ia != NULL)
1678                         ifa_free(&ia->ia_ifa);
1679                 return;
1680         }
1681         if (ia != NULL)
1682                 ifa_free(&ia->ia_ifa);
1683         icmp_error(mcopy, type, code, dest.s_addr, mtu);
1684 }
1685
1686 void
1687 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1688     struct mbuf *m)
1689 {
1690
1691         if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1692                 struct bintime bt;
1693
1694                 bintime(&bt);
1695                 if (inp->inp_socket->so_options & SO_BINTIME) {
1696                         *mp = sbcreatecontrol((caddr_t)&bt, sizeof(bt),
1697                             SCM_BINTIME, SOL_SOCKET);
1698                         if (*mp)
1699                                 mp = &(*mp)->m_next;
1700                 }
1701                 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1702                         struct timeval tv;
1703
1704                         bintime2timeval(&bt, &tv);
1705                         *mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv),
1706                             SCM_TIMESTAMP, SOL_SOCKET);
1707                         if (*mp)
1708                                 mp = &(*mp)->m_next;
1709                 }
1710         }
1711         if (inp->inp_flags & INP_RECVDSTADDR) {
1712                 *mp = sbcreatecontrol((caddr_t)&ip->ip_dst,
1713                     sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1714                 if (*mp)
1715                         mp = &(*mp)->m_next;
1716         }
1717         if (inp->inp_flags & INP_RECVTTL) {
1718                 *mp = sbcreatecontrol((caddr_t)&ip->ip_ttl,
1719                     sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
1720                 if (*mp)
1721                         mp = &(*mp)->m_next;
1722         }
1723 #ifdef notyet
1724         /* XXX
1725          * Moving these out of udp_input() made them even more broken
1726          * than they already were.
1727          */
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);
1732                 if (*mp)
1733                         mp = &(*mp)->m_next;
1734         }
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);
1739                 if (*mp)
1740                         mp = &(*mp)->m_next;
1741         }
1742 #endif
1743         if (inp->inp_flags & INP_RECVIF) {
1744                 struct ifnet *ifp;
1745                 struct sdlbuf {
1746                         struct sockaddr_dl sdl;
1747                         u_char  pad[32];
1748                 } sdlbuf;
1749                 struct sockaddr_dl *sdp;
1750                 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1751
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;
1755                         /*
1756                          * Change our mind and don't try copy.
1757                          */
1758                         if (sdp->sdl_family != AF_LINK ||
1759                             sdp->sdl_len > sizeof(sdlbuf)) {
1760                                 goto makedummy;
1761                         }
1762                         bcopy(sdp, sdl2, sdp->sdl_len);
1763                 } else {
1764 makedummy:      
1765                         sdl2->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;
1770                 }
1771                 *mp = sbcreatecontrol((caddr_t)sdl2, sdl2->sdl_len,
1772                     IP_RECVIF, IPPROTO_IP);
1773                 if (*mp)
1774                         mp = &(*mp)->m_next;
1775         }
1776         if (inp->inp_flags & INP_RECVTOS) {
1777                 *mp = sbcreatecontrol((caddr_t)&ip->ip_tos,
1778                     sizeof(u_char), IP_RECVTOS, IPPROTO_IP);
1779                 if (*mp)
1780                         mp = &(*mp)->m_next;
1781         }
1782
1783         if (inp->inp_flags2 & INP_RECVFLOWID) {
1784                 uint32_t flowid, flow_type;
1785
1786                 flowid = m->m_pkthdr.flowid;
1787                 flow_type = M_HASHTYPE_GET(m);
1788
1789                 /*
1790                  * XXX should handle the failure of one or the
1791                  * other - don't populate both?
1792                  */
1793                 *mp = sbcreatecontrol((caddr_t) &flowid,
1794                     sizeof(uint32_t), IP_FLOWID, IPPROTO_IP);
1795                 if (*mp)
1796                         mp = &(*mp)->m_next;
1797                 *mp = sbcreatecontrol((caddr_t) &flow_type,
1798                     sizeof(uint32_t), IP_FLOWTYPE, IPPROTO_IP);
1799                 if (*mp)
1800                         mp = &(*mp)->m_next;
1801         }
1802
1803 #ifdef  RSS
1804         if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
1805                 uint32_t flowid, flow_type;
1806                 uint32_t rss_bucketid;
1807
1808                 flowid = m->m_pkthdr.flowid;
1809                 flow_type = M_HASHTYPE_GET(m);
1810
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);
1814                         if (*mp)
1815                                 mp = &(*mp)->m_next;
1816                 }
1817         }
1818 #endif
1819 }
1820
1821 /*
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
1825  * compiled.
1826  */
1827 static VNET_DEFINE(int, ip_rsvp_on);
1828 VNET_DEFINE(struct socket *, ip_rsvpd);
1829
1830 #define V_ip_rsvp_on            VNET(ip_rsvp_on)
1831
1832 int
1833 ip_rsvp_init(struct socket *so)
1834 {
1835
1836         if (so->so_type != SOCK_RAW ||
1837             so->so_proto->pr_protocol != IPPROTO_RSVP)
1838                 return EOPNOTSUPP;
1839
1840         if (V_ip_rsvpd != NULL)
1841                 return EADDRINUSE;
1842
1843         V_ip_rsvpd = so;
1844         /*
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.
1847          */
1848         if (!V_ip_rsvp_on) {
1849                 V_ip_rsvp_on = 1;
1850                 V_rsvp_on++;
1851         }
1852
1853         return 0;
1854 }
1855
1856 int
1857 ip_rsvp_done(void)
1858 {
1859
1860         V_ip_rsvpd = NULL;
1861         /*
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.
1864          */
1865         if (V_ip_rsvp_on) {
1866                 V_ip_rsvp_on = 0;
1867                 V_rsvp_on--;
1868         }
1869         return 0;
1870 }
1871
1872 int
1873 rsvp_input(struct mbuf **mp, int *offp, int proto)
1874 {
1875         struct mbuf *m;
1876
1877         m = *mp;
1878         *mp = NULL;
1879
1880         if (rsvp_input_p) { /* call the real one if loaded */
1881                 *mp = m;
1882                 rsvp_input_p(mp, offp, proto);
1883                 return (IPPROTO_DONE);
1884         }
1885
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.
1889          */
1890         
1891         if (!V_rsvp_on) {
1892                 m_freem(m);
1893                 return (IPPROTO_DONE);
1894         }
1895
1896         if (V_ip_rsvpd != NULL) { 
1897                 *mp = m;
1898                 rip_input(mp, offp, proto);
1899                 return (IPPROTO_DONE);
1900         }
1901         /* Drop the packet */
1902         m_freem(m);
1903         return (IPPROTO_DONE);
1904 }