1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_bootp.h"
38 #include "opt_inet.h"
39 #include "opt_ipstealth.h"
40 #include "opt_ipsec.h"
41 #include "opt_route.h"
42 #include "opt_rss.h"
43 #include "opt_sctp.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/hhook.h>
48 #include <sys/mbuf.h>
49 #include <sys/malloc.h>
50 #include <sys/domain.h>
51 #include <sys/protosw.h>
52 #include <sys/socket.h>
53 #include <sys/time.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/rmlock.h>
57 #include <sys/rwlock.h>
58 #include <sys/sdt.h>
59 #include <sys/syslog.h>
60 #include <sys/sysctl.h>
61
62 #include <net/if.h>
63 #include <net/if_types.h>
64 #include <net/if_var.h>
65 #include <net/if_dl.h>
66 #include <net/pfil.h>
67 #include <net/route.h>
68 #include <net/route/nhop.h>
69 #include <net/netisr.h>
70 #include <net/rss_config.h>
71 #include <net/vnet.h>
72
73 #include <netinet/in.h>
74 #include <netinet/in_kdtrace.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip.h>
78 #include <netinet/in_fib.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/ip_var.h>
81 #include <netinet/ip_encap.h>
82 #include <netinet/ip_fw.h>
83 #include <netinet/ip_icmp.h>
84 #include <netinet/igmp_var.h>
85 #include <netinet/ip_options.h>
86 #include <machine/in_cksum.h>
87 #include <netinet/ip_carp.h>
88 #include <netinet/in_rss.h>
89 #ifdef SCTP
90 #include <netinet/sctp_var.h>
91 #endif
92
93 #include <netipsec/ipsec_support.h>
94
95 #include <sys/socketvar.h>
96
97 #include <security/mac/mac_framework.h>
98
99 #ifdef CTASSERT
100 CTASSERT(sizeof(struct ip) == 20);
101 #endif
102
103 /* IP reassembly functions are defined in ip_reass.c. */
104 extern void ipreass_init(void);
105 extern void ipreass_vnet_init(void);
106 #ifdef VIMAGE
107 extern void ipreass_destroy(void);
108 #endif
109
110 VNET_DEFINE(int, rsvp_on);
111
112 VNET_DEFINE(int, ipforwarding);
113 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_VNET | CTLFLAG_RW,
114 &VNET_NAME(ipforwarding), 0,
115 "Enable IP forwarding between interfaces");
116
117 /*
118 * Respond with an ICMP host redirect when we forward a packet out of
119 * the same interface on which it was received. See RFC 792.
120 */
121 VNET_DEFINE(int, ipsendredirects) = 1;
122 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_VNET | CTLFLAG_RW,
123 &VNET_NAME(ipsendredirects), 0,
124 "Enable sending IP redirects");
125
126 VNET_DEFINE_STATIC(bool, ip_strong_es) = false;
127 #define V_ip_strong_es VNET(ip_strong_es)
128 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, rfc1122_strong_es,
129 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_strong_es), false,
130 "Packet's IP destination address must match address on arrival interface");
131
132 VNET_DEFINE_STATIC(bool, ip_sav) = true;
133 #define V_ip_sav VNET(ip_sav)
134 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, source_address_validation,
135 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_sav), true,
136 "Drop incoming packets with source address that is a local address");
137
138 VNET_DEFINE(pfil_head_t, inet_pfil_head); /* Packet filter hooks */
139
140 static struct netisr_handler ip_nh = {
141 .nh_name = "ip",
142 .nh_handler = ip_input,
143 .nh_proto = NETISR_IP,
144 #ifdef RSS
145 .nh_m2cpuid = rss_soft_m2cpuid_v4,
146 .nh_policy = NETISR_POLICY_CPU,
147 .nh_dispatch = NETISR_DISPATCH_HYBRID,
148 #else
149 .nh_policy = NETISR_POLICY_FLOW,
150 #endif
151 };
152
153 #ifdef RSS
154 /*
155 * Directly dispatched frames are currently assumed
156 * to have a flowid already calculated.
157 *
158 * It should likely have something that assert it
159 * actually has valid flow details.
160 */
161 static struct netisr_handler ip_direct_nh = {
162 .nh_name = "ip_direct",
163 .nh_handler = ip_direct_input,
164 .nh_proto = NETISR_IP_DIRECT,
165 .nh_m2cpuid = rss_soft_m2cpuid_v4,
166 .nh_policy = NETISR_POLICY_CPU,
167 .nh_dispatch = NETISR_DISPATCH_HYBRID,
168 };
169 #endif
170
171 ipproto_input_t *ip_protox[IPPROTO_MAX] = {
172 [0 ... IPPROTO_MAX - 1] = rip_input };
173 ipproto_ctlinput_t *ip_ctlprotox[IPPROTO_MAX] = {
174 [0 ... IPPROTO_MAX - 1] = rip_ctlinput };
175
176 VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead); /* first inet address */
177 VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table */
178 VNET_DEFINE(u_long, in_ifaddrhmask); /* mask for hash table */
179
180 /* Make sure it is safe to use hashinit(9) on CK_LIST. */
181 CTASSERT(sizeof(struct in_ifaddrhashhead) == sizeof(LIST_HEAD(, in_addr)));
182
183 #ifdef IPCTL_DEFMTU
184 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
185 &ip_mtu, 0, "Default MTU");
186 #endif
187
188 #ifdef IPSTEALTH
189 VNET_DEFINE(int, ipstealth);
190 SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_VNET | CTLFLAG_RW,
191 &VNET_NAME(ipstealth), 0,
192 "IP stealth mode, no TTL decrementation on forwarding");
193 #endif
194
195 /*
196 * IP statistics are stored in the "array" of counter(9)s.
197 */
198 VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat);
199 VNET_PCPUSTAT_SYSINIT(ipstat);
200 SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat,
201 "IP statistics (struct ipstat, netinet/ip_var.h)");
202
203 #ifdef VIMAGE
204 VNET_PCPUSTAT_SYSUNINIT(ipstat);
205 #endif /* VIMAGE */
206
207 /*
208 * Kernel module interface for updating ipstat. The argument is an index
209 * into ipstat treated as an array.
210 */
211 void
212 kmod_ipstat_inc(int statnum)
213 {
214
215 counter_u64_add(VNET(ipstat)[statnum], 1);
216 }
217
218 void
219 kmod_ipstat_dec(int statnum)
220 {
221
222 counter_u64_add(VNET(ipstat)[statnum], -1);
223 }
224
225 static int
226 sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
227 {
228 int error, qlimit;
229
230 netisr_getqlimit(&ip_nh, &qlimit);
231 error = sysctl_handle_int(oidp, &qlimit, 0, req);
232 if (error || !req->newptr)
233 return (error);
234 if (qlimit < 1)
235 return (EINVAL);
236 return (netisr_setqlimit(&ip_nh, qlimit));
237 }
238 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen,
239 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
240 sysctl_netinet_intr_queue_maxlen, "I",
241 "Maximum size of the IP input queue");
242
243 static int
244 sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
245 {
246 u_int64_t qdrops_long;
247 int error, qdrops;
248
249 netisr_getqdrops(&ip_nh, &qdrops_long);
250 qdrops = qdrops_long;
251 error = sysctl_handle_int(oidp, &qdrops, 0, req);
252 if (error || !req->newptr)
253 return (error);
254 if (qdrops != 0)
255 return (EINVAL);
256 netisr_clearqdrops(&ip_nh);
257 return (0);
258 }
259
260 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops,
261 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
262 0, 0, sysctl_netinet_intr_queue_drops, "I",
263 "Number of packets dropped from the IP input queue");
264
265 #ifdef RSS
266 static int
267 sysctl_netinet_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
268 {
269 int error, qlimit;
270
271 netisr_getqlimit(&ip_direct_nh, &qlimit);
272 error = sysctl_handle_int(oidp, &qlimit, 0, req);
273 if (error || !req->newptr)
274 return (error);
275 if (qlimit < 1)
276 return (EINVAL);
277 return (netisr_setqlimit(&ip_direct_nh, qlimit));
278 }
279 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRDQMAXLEN, intr_direct_queue_maxlen,
280 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
281 0, 0, sysctl_netinet_intr_direct_queue_maxlen,
282 "I", "Maximum size of the IP direct input queue");
283
284 static int
285 sysctl_netinet_intr_direct_queue_drops(SYSCTL_HANDLER_ARGS)
286 {
287 u_int64_t qdrops_long;
288 int error, qdrops;
289
290 netisr_getqdrops(&ip_direct_nh, &qdrops_long);
291 qdrops = qdrops_long;
292 error = sysctl_handle_int(oidp, &qdrops, 0, req);
293 if (error || !req->newptr)
294 return (error);
295 if (qdrops != 0)
296 return (EINVAL);
297 netisr_clearqdrops(&ip_direct_nh);
298 return (0);
299 }
300
301 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRDQDROPS, intr_direct_queue_drops,
302 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
303 sysctl_netinet_intr_direct_queue_drops, "I",
304 "Number of packets dropped from the IP direct input queue");
305 #endif /* RSS */
306
307 /*
308 * IP initialization: fill in IP protocol switch table.
309 * All protocols not implemented in kernel go to raw IP protocol handler.
310 */
311 static void
312 ip_vnet_init(void *arg __unused)
313 {
314 struct pfil_head_args args;
315
316 CK_STAILQ_INIT(&V_in_ifaddrhead);
317 V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
318
319 /* Initialize IP reassembly queue. */
320 ipreass_vnet_init();
321
322 /* Initialize packet filter hooks. */
323 args.pa_version = PFIL_VERSION;
324 args.pa_flags = PFIL_IN | PFIL_OUT;
325 args.pa_type = PFIL_TYPE_IP4;
326 args.pa_headname = PFIL_INET_NAME;
327 V_inet_pfil_head = pfil_head_register(&args);
328
329 if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET,
330 &V_ipsec_hhh_in[HHOOK_IPSEC_INET],
331 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
332 printf("%s: WARNING: unable to register input helper hook\n",
333 __func__);
334 if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET,
335 &V_ipsec_hhh_out[HHOOK_IPSEC_INET],
336 HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
337 printf("%s: WARNING: unable to register output helper hook\n",
338 __func__);
339
340 #ifdef VIMAGE
341 netisr_register_vnet(&ip_nh);
342 #ifdef RSS
343 netisr_register_vnet(&ip_direct_nh);
344 #endif
345 #endif
346 }
347 VNET_SYSINIT(ip_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
348 ip_vnet_init, NULL);
349
350 static void
351 ip_init(const void *unused __unused)
352 {
353
354 ipreass_init();
355
356 /*
357 * Register statically compiled protocols, that are unlikely to
358 * ever become dynamic.
359 */
360 IPPROTO_REGISTER(IPPROTO_ICMP, icmp_input, NULL);
361 IPPROTO_REGISTER(IPPROTO_IGMP, igmp_input, NULL);
362 IPPROTO_REGISTER(IPPROTO_RSVP, rsvp_input, NULL);
363 IPPROTO_REGISTER(IPPROTO_IPV4, encap4_input, NULL);
364 IPPROTO_REGISTER(IPPROTO_MOBILE, encap4_input, NULL);
365 IPPROTO_REGISTER(IPPROTO_ETHERIP, encap4_input, NULL);
366 IPPROTO_REGISTER(IPPROTO_GRE, encap4_input, NULL);
367 IPPROTO_REGISTER(IPPROTO_IPV6, encap4_input, NULL);
368 IPPROTO_REGISTER(IPPROTO_PIM, encap4_input, NULL);
369 #ifdef SCTP /* XXX: has a loadable & static version */
370 IPPROTO_REGISTER(IPPROTO_SCTP, sctp_input, sctp_ctlinput);
371 #endif
372
373 netisr_register(&ip_nh);
374 #ifdef RSS
375 netisr_register(&ip_direct_nh);
376 #endif
377 }
378 SYSINIT(ip_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip_init, NULL);
379
380 #ifdef VIMAGE
381 static void
382 ip_destroy(void *unused __unused)
383 {
384 int error;
385
386 #ifdef RSS
387 netisr_unregister_vnet(&ip_direct_nh);
388 #endif
389 netisr_unregister_vnet(&ip_nh);
390
391 pfil_head_unregister(V_inet_pfil_head);
392 error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET]);
393 if (error != 0) {
394 printf("%s: WARNING: unable to deregister input helper hook "
395 "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET: "
396 "error %d returned\n", __func__, error);
397 }
398 error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET]);
399 if (error != 0) {
400 printf("%s: WARNING: unable to deregister output helper hook "
401 "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET: "
402 "error %d returned\n", __func__, error);
403 }
404
405 /* Remove the IPv4 addresses from all interfaces. */
406 in_ifscrub_all();
407
408 /* Make sure the IPv4 routes are gone as well. */
409 rib_flush_routes_family(AF_INET);
410
411 /* Destroy IP reassembly queue. */
412 ipreass_destroy();
413
414 /* Cleanup in_ifaddr hash table; should be empty. */
415 hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask);
416 }
417
418 VNET_SYSUNINIT(ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip_destroy, NULL);
419 #endif
420
421 #ifdef RSS
422 /*
423 * IP direct input routine.
424 *
425 * This is called when reinjecting completed fragments where
426 * all of the previous checking and book-keeping has been done.
427 */
428 void
429 ip_direct_input(struct mbuf *m)
430 {
431 struct ip *ip;
432 int hlen;
433
434 ip = mtod(m, struct ip *);
435 hlen = ip->ip_hl << 2;
436
437 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
438 if (IPSEC_ENABLED(ipv4)) {
439 if (IPSEC_INPUT(ipv4, m, hlen, ip->ip_p) != 0)
440 return;
441 }
442 #endif /* IPSEC */
443 IPSTAT_INC(ips_delivered);
444 ip_protox[ip->ip_p](&m, &hlen, ip->ip_p);
445 }
446 #endif
447
448 /*
449 * Ip input routine. Checksum and byte swap header. If fragmented
450 * try to reassemble. Process options. Pass to next level.
451 */
452 void
453 ip_input(struct mbuf *m)
454 {
455 struct ip *ip = NULL;
456 struct in_ifaddr *ia = NULL;
457 struct ifaddr *ifa;
458 struct ifnet *ifp;
459 int hlen = 0;
460 uint16_t sum, ip_len;
461 int dchg = 0; /* dest changed after fw */
462 struct in_addr odst; /* original dst address */
463 bool strong_es;
464
465 M_ASSERTPKTHDR(m);
466 NET_EPOCH_ASSERT();
467
468 if (m->m_flags & M_FASTFWD_OURS) {
469 m->m_flags &= ~M_FASTFWD_OURS;
470 /* Set up some basics that will be used later. */
471 ip = mtod(m, struct ip *);
472 hlen = ip->ip_hl << 2;
473 ip_len = ntohs(ip->ip_len);
474 goto ours;
475 }
476
477 IPSTAT_INC(ips_total);
478
479 if (__predict_false(m->m_pkthdr.len < sizeof(struct ip)))
480 goto tooshort;
481
482 if (m->m_len < sizeof(struct ip)) {
483 m = m_pullup(m, sizeof(struct ip));
484 if (__predict_false(m == NULL)) {
485 IPSTAT_INC(ips_toosmall);
486 return;
487 }
488 }
489 ip = mtod(m, struct ip *);
490
491 if (__predict_false(ip->ip_v != IPVERSION)) {
492 IPSTAT_INC(ips_badvers);
493 goto bad;
494 }
495
496 hlen = ip->ip_hl << 2;
497 if (__predict_false(hlen < sizeof(struct ip))) { /* minimum header length */
498 IPSTAT_INC(ips_badhlen);
499 goto bad;
500 }
501 if (hlen > m->m_len) {
502 m = m_pullup(m, hlen);
503 if (__predict_false(m == NULL)) {
504 IPSTAT_INC(ips_badhlen);
505 return;
506 }
507 ip = mtod(m, struct ip *);
508 }
509
510 IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL);
511
512 /* IN_LOOPBACK must not appear on the wire - RFC1122 */
513 ifp = m->m_pkthdr.rcvif;
514 if (IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) ||
515 IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) {
516 if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
517 IPSTAT_INC(ips_badaddr);
518 goto bad;
519 }
520 }
521
522 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
523 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
524 } else {
525 if (hlen == sizeof(struct ip)) {
526 sum = in_cksum_hdr(ip);
527 } else {
528 sum = in_cksum(m, hlen);
529 }
530 }
531 if (__predict_false(sum)) {
532 IPSTAT_INC(ips_badsum);
533 goto bad;
534 }
535
536 ip_len = ntohs(ip->ip_len);
537 if (__predict_false(ip_len < hlen)) {
538 IPSTAT_INC(ips_badlen);
539 goto bad;
540 }
541
542 /*
543 * Check that the amount of data in the buffers
544 * is as at least much as the IP header would have us expect.
545 * Trim mbufs if longer than we expect.
546 * Drop packet if shorter than we expect.
547 */
548 if (__predict_false(m->m_pkthdr.len < ip_len)) {
549 tooshort:
550 IPSTAT_INC(ips_tooshort);
551 goto bad;
552 }
553 if (m->m_pkthdr.len > ip_len) {
554 if (m->m_len == m->m_pkthdr.len) {
555 m->m_len = ip_len;
556 m->m_pkthdr.len = ip_len;
557 } else
558 m_adj(m, ip_len - m->m_pkthdr.len);
559 }
560
561 /*
562 * Try to forward the packet, but if we fail continue.
563 * ip_tryforward() may generate redirects these days.
564 * XXX the logic below falling through to normal processing
565 * if redirects are required should be revisited as well.
566 * ip_tryforward() does inbound and outbound packet firewall
567 * processing. If firewall has decided that destination becomes
568 * our local address, it sets M_FASTFWD_OURS flag. In this
569 * case skip another inbound firewall processing and update
570 * ip pointer.
571 */
572 if (V_ipforwarding != 0
573 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
574 && (!IPSEC_ENABLED(ipv4) ||
575 IPSEC_CAPS(ipv4, m, IPSEC_CAP_OPERABLE) == 0)
576 #endif
577 ) {
578 /*
579 * ip_dooptions() was run so we can ignore the source route (or
580 * any IP options case) case for redirects in ip_tryforward().
581 */
582 if ((m = ip_tryforward(m)) == NULL)
583 return;
584 if (m->m_flags & M_FASTFWD_OURS) {
585 m->m_flags &= ~M_FASTFWD_OURS;
586 ip = mtod(m, struct ip *);
587 goto ours;
588 }
589 }
590
591 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
592 /*
593 * Bypass packet filtering for packets previously handled by IPsec.
594 */
595 if (IPSEC_ENABLED(ipv4) &&
596 IPSEC_CAPS(ipv4, m, IPSEC_CAP_BYPASS_FILTER) != 0)
597 goto passin;
598 #endif
599
600 /*
601 * Run through list of hooks for input packets.
602 *
603 * NB: Beware of the destination address changing (e.g.
604 * by NAT rewriting). When this happens, tell
605 * ip_forward to do the right thing.
606 */
607
608 /* Jump over all PFIL processing if hooks are not active. */
609 if (!PFIL_HOOKED_IN(V_inet_pfil_head))
610 goto passin;
611
612 odst = ip->ip_dst;
613 if (pfil_mbuf_in(V_inet_pfil_head, &m, ifp, NULL) !=
614 PFIL_PASS)
615 return;
616 if (m == NULL) /* consumed by filter */
617 return;
618
619 ip = mtod(m, struct ip *);
620 dchg = (odst.s_addr != ip->ip_dst.s_addr);
621
622 if (m->m_flags & M_FASTFWD_OURS) {
623 m->m_flags &= ~M_FASTFWD_OURS;
624 goto ours;
625 }
626 if (m->m_flags & M_IP_NEXTHOP) {
627 if (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
628 /*
629 * Directly ship the packet on. This allows
630 * forwarding packets originally destined to us
631 * to some other directly connected host.
632 */
633 ip_forward(m, 1);
634 return;
635 }
636 }
637 passin:
638
639 /*
640 * Process options and, if not destined for us,
641 * ship it on. ip_dooptions returns 1 when an
642 * error was detected (causing an icmp message
643 * to be sent and the original packet to be freed).
644 */
645 if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
646 return;
647
648 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
649 * matter if it is destined to another node, or whether it is
650 * a multicast one, RSVP wants it! and prevents it from being forwarded
651 * anywhere else. Also checks if the rsvp daemon is running before
652 * grabbing the packet.
653 */
654 if (ip->ip_p == IPPROTO_RSVP && V_rsvp_on)
655 goto ours;
656
657 /*
658 * Check our list of addresses, to see if the packet is for us.
659 * If we don't have any addresses, assume any unicast packet
660 * we receive might be for us (and let the upper layers deal
661 * with it).
662 */
663 if (CK_STAILQ_EMPTY(&V_in_ifaddrhead) &&
664 (m->m_flags & (M_MCAST|M_BCAST)) == 0)
665 goto ours;
666
667 /*
668 * Enable a consistency check between the destination address
669 * and the arrival interface for a unicast packet (the RFC 1122
670 * strong ES model) with a list of additional predicates:
671 * - if IP forwarding is disabled
672 * - the packet is not locally generated
673 * - the packet is not subject to 'ipfw fwd'
674 * - Interface is not running CARP. If the packet got here, we already
675 * checked it with carp_iamatch() and carp_forus().
676 */
677 strong_es = V_ip_strong_es && (V_ipforwarding == 0) &&
678 ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
679 ifp->if_carp == NULL && (dchg == 0);
680
681 /*
682 * Check for exact addresses in the hash bucket.
683 */
684 CK_LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
685 if (IA_SIN(ia)->sin_addr.s_addr != ip->ip_dst.s_addr)
686 continue;
687
688 /*
689 * net.inet.ip.rfc1122_strong_es: the address matches, verify
690 * that the packet arrived via the correct interface.
691 */
692 if (__predict_false(strong_es && ia->ia_ifp != ifp)) {
693 IPSTAT_INC(ips_badaddr);
694 goto bad;
695 }
696
697 /*
698 * net.inet.ip.source_address_validation: drop incoming
699 * packets that pretend to be ours.
700 */
701 if (V_ip_sav && !(ifp->if_flags & IFF_LOOPBACK) &&
702 __predict_false(in_localip_fib(ip->ip_src, ifp->if_fib))) {
703 IPSTAT_INC(ips_badaddr);
704 goto bad;
705 }
706
707 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
708 counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len);
709 goto ours;
710 }
711
712 /*
713 * Check for broadcast addresses.
714 *
715 * Only accept broadcast packets that arrive via the matching
716 * interface. Reception of forwarded directed broadcasts would
717 * be handled via ip_forward() and ether_output() with the loopback
718 * into the stack for SIMPLEX interfaces handled by ether_output().
719 */
720 if (ifp->if_flags & IFF_BROADCAST) {
721 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
722 if (ifa->ifa_addr->sa_family != AF_INET)
723 continue;
724 ia = ifatoia(ifa);
725 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
726 ip->ip_dst.s_addr) {
727 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
728 counter_u64_add(ia->ia_ifa.ifa_ibytes,
729 m->m_pkthdr.len);
730 goto ours;
731 }
732 #ifdef BOOTP_COMPAT
733 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
734 counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
735 counter_u64_add(ia->ia_ifa.ifa_ibytes,
736 m->m_pkthdr.len);
737 goto ours;
738 }
739 #endif
740 }
741 ia = NULL;
742 }
743 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
744 /*
745 * RFC 3927 2.7: Do not forward multicast packets from
746 * IN_LINKLOCAL.
747 */
748 if (V_ip_mrouter && !IN_LINKLOCAL(ntohl(ip->ip_src.s_addr))) {
749 /*
750 * If we are acting as a multicast router, all
751 * incoming multicast packets are passed to the
752 * kernel-level multicast forwarding function.
753 * The packet is returned (relatively) intact; if
754 * ip_mforward() returns a non-zero value, the packet
755 * must be discarded, else it may be accepted below.
756 */
757 if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
758 IPSTAT_INC(ips_cantforward);
759 m_freem(m);
760 return;
761 }
762
763 /*
764 * The process-level routing daemon needs to receive
765 * all multicast IGMP packets, whether or not this
766 * host belongs to their destination groups.
767 */
768 if (ip->ip_p == IPPROTO_IGMP) {
769 goto ours;
770 }
771 IPSTAT_INC(ips_forward);
772 }
773 /*
774 * Assume the packet is for us, to avoid prematurely taking
775 * a lock on the in_multi hash. Protocols must perform
776 * their own filtering and update statistics accordingly.
777 */
778 goto ours;
779 }
780 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
781 goto ours;
782 if (ip->ip_dst.s_addr == INADDR_ANY)
783 goto ours;
784 /* RFC 3927 2.7: Do not forward packets to or from IN_LINKLOCAL. */
785 if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
786 IN_LINKLOCAL(ntohl(ip->ip_src.s_addr))) {
787 IPSTAT_INC(ips_cantforward);
788 m_freem(m);
789 return;
790 }
791
792 /*
793 * Not for us; forward if possible and desirable.
794 */
795 if (V_ipforwarding == 0) {
796 IPSTAT_INC(ips_cantforward);
797 m_freem(m);
798 } else {
799 ip_forward(m, dchg);
800 }
801 return;
802
803 ours:
804 #ifdef IPSTEALTH
805 /*
806 * IPSTEALTH: Process non-routing options only
807 * if the packet is destined for us.
808 */
809 if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1))
810 return;
811 #endif /* IPSTEALTH */
812
813 /*
814 * Attempt reassembly; if it succeeds, proceed.
815 * ip_reass() will return a different mbuf.
816 */
817 if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
818 /* XXXGL: shouldn't we save & set m_flags? */
819 m = ip_reass(m);
820 if (m == NULL)
821 return;
822 ip = mtod(m, struct ip *);
823 /* Get the header length of the reassembled packet */
824 hlen = ip->ip_hl << 2;
825 }
826
827 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
828 if (IPSEC_ENABLED(ipv4)) {
829 if (IPSEC_INPUT(ipv4, m, hlen, ip->ip_p) != 0)
830 return;
831 }
832 #endif /* IPSEC */
833
834 /*
835 * Switch out to protocol's input routine.
836 */
837 IPSTAT_INC(ips_delivered);
838
839 ip_protox[ip->ip_p](&m, &hlen, ip->ip_p);
840 return;
841 bad:
842 m_freem(m);
843 }
844
845 int
846 ipproto_register(uint8_t proto, ipproto_input_t input, ipproto_ctlinput_t ctl)
847 {
848
849 MPASS(proto > 0);
850
851 /*
852 * The protocol slot must not be occupied by another protocol
853 * already. An index pointing to rip_input() is unused.
854 */
855 if (ip_protox[proto] == rip_input) {
856 ip_protox[proto] = input;
857 ip_ctlprotox[proto] = ctl;
858 return (0);
859 } else
860 return (EEXIST);
861 }
862
863 int
864 ipproto_unregister(uint8_t proto)
865 {
866
867 MPASS(proto > 0);
868
869 if (ip_protox[proto] != rip_input) {
870 ip_protox[proto] = rip_input;
871 ip_ctlprotox[proto] = rip_ctlinput;
872 return (0);
873 } else
874 return (ENOENT);
875 }
876
877 /*
878 * Forward a packet. If some error occurs return the sender
879 * an icmp packet. Note we can't always generate a meaningful
880 * icmp message because icmp doesn't have a large enough repertoire
881 * of codes and types.
882 *
883 * If not forwarding, just drop the packet. This could be confusing
884 * if ipforwarding was zero but some routing protocol was advancing
885 * us as a gateway to somewhere. However, we must let the routing
886 * protocol deal with that.
887 *
888 * The srcrt parameter indicates whether the packet is being forwarded
889 * via a source route.
890 */
891 void
892 ip_forward(struct mbuf *m, int srcrt)
893 {
894 struct ip *ip = mtod(m, struct ip *);
895 struct in_ifaddr *ia;
896 struct mbuf *mcopy;
897 struct sockaddr_in *sin;
898 struct in_addr dest;
899 struct route ro;
900 uint32_t flowid;
901 int error, type = 0, code = 0, mtu = 0;
902
903 NET_EPOCH_ASSERT();
904
905 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
906 IPSTAT_INC(ips_cantforward);
907 m_freem(m);
908 return;
909 }
910 if (
911 #ifdef IPSTEALTH
912 V_ipstealth == 0 &&
913 #endif
914 ip->ip_ttl <= IPTTLDEC) {
915 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
916 return;
917 }
918
919 bzero(&ro, sizeof(ro));
920 sin = (struct sockaddr_in *)&ro.ro_dst;
921 sin->sin_family = AF_INET;
922 sin->sin_len = sizeof(*sin);
923 sin->sin_addr = ip->ip_dst;
924 flowid = m->m_pkthdr.flowid;
925 ro.ro_nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_REF, flowid);
926 if (ro.ro_nh != NULL) {
927 ia = ifatoia(ro.ro_nh->nh_ifa);
928 } else
929 ia = NULL;
930 /*
931 * Save the IP header and at most 8 bytes of the payload,
932 * in case we need to generate an ICMP message to the src.
933 *
934 * XXX this can be optimized a lot by saving the data in a local
935 * buffer on the stack (72 bytes at most), and only allocating the
936 * mbuf if really necessary. The vast majority of the packets
937 * are forwarded without having to send an ICMP back (either
938 * because unnecessary, or because rate limited), so we are
939 * really we are wasting a lot of work here.
940 *
941 * We don't use m_copym() because it might return a reference
942 * to a shared cluster. Both this function and ip_output()
943 * assume exclusive access to the IP header in `m', so any
944 * data in a cluster may change before we reach icmp_error().
945 */
946 mcopy = m_gethdr(M_NOWAIT, m->m_type);
947 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
948 /*
949 * It's probably ok if the pkthdr dup fails (because
950 * the deep copy of the tag chain failed), but for now
951 * be conservative and just discard the copy since
952 * code below may some day want the tags.
953 */
954 m_free(mcopy);
955 mcopy = NULL;
956 }
957 if (mcopy != NULL) {
958 mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy));
959 mcopy->m_pkthdr.len = mcopy->m_len;
960 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
961 }
962 #ifdef IPSTEALTH
963 if (V_ipstealth == 0)
964 #endif
965 ip->ip_ttl -= IPTTLDEC;
966 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
967 if (IPSEC_ENABLED(ipv4)) {
968 if ((error = IPSEC_FORWARD(ipv4, m)) != 0) {
969 /* mbuf consumed by IPsec */
970 RO_NHFREE(&ro);
971 m_freem(mcopy);
972 if (error != EINPROGRESS)
973 IPSTAT_INC(ips_cantforward);
974 return;
975 }
976 /* No IPsec processing required */
977 }
978 #endif /* IPSEC */
979 /*
980 * If forwarding packet using same interface that it came in on,
981 * perhaps should send a redirect to sender to shortcut a hop.
982 * Only send redirect if source is sending directly to us,
983 * and if packet was not source routed (or has any options).
984 * Also, don't send redirect if forwarding using a default route
985 * or a route modified by a redirect.
986 */
987 dest.s_addr = 0;
988 if (!srcrt && V_ipsendredirects &&
989 ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
990 struct nhop_object *nh;
991
992 nh = ro.ro_nh;
993
994 if (nh != NULL && ((nh->nh_flags & (NHF_REDIRECT|NHF_DEFAULT)) == 0)) {
995 struct in_ifaddr *nh_ia = (struct in_ifaddr *)(nh->nh_ifa);
996 u_long src = ntohl(ip->ip_src.s_addr);
997
998 if (nh_ia != NULL &&
999 (src & nh_ia->ia_subnetmask) == nh_ia->ia_subnet) {
1000 /* Router requirements says to only send host redirects */
1001 type = ICMP_REDIRECT;
1002 code = ICMP_REDIRECT_HOST;
1003 if (nh->nh_flags & NHF_GATEWAY) {
1004 if (nh->gw_sa.sa_family == AF_INET)
1005 dest.s_addr = nh->gw4_sa.sin_addr.s_addr;
1006 else /* Do not redirect in case gw is AF_INET6 */
1007 type = 0;
1008 } else
1009 dest.s_addr = ip->ip_dst.s_addr;
1010 }
1011 }
1012 }
1013
1014 error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1015
1016 if (error == EMSGSIZE && ro.ro_nh)
1017 mtu = ro.ro_nh->nh_mtu;
1018 RO_NHFREE(&ro);
1019
1020 if (error)
1021 IPSTAT_INC(ips_cantforward);
1022 else {
1023 IPSTAT_INC(ips_forward);
1024 if (type)
1025 IPSTAT_INC(ips_redirectsent);
1026 else {
1027 if (mcopy)
1028 m_freem(mcopy);
1029 return;
1030 }
1031 }
1032 if (mcopy == NULL)
1033 return;
1034
1035 switch (error) {
1036 case 0: /* forwarded, but need redirect */
1037 /* type, code set above */
1038 break;
1039
1040 case ENETUNREACH:
1041 case EHOSTUNREACH:
1042 case ENETDOWN:
1043 case EHOSTDOWN:
1044 default:
1045 type = ICMP_UNREACH;
1046 code = ICMP_UNREACH_HOST;
1047 break;
1048
1049 case EMSGSIZE:
1050 type = ICMP_UNREACH;
1051 code = ICMP_UNREACH_NEEDFRAG;
1052 /*
1053 * If the MTU was set before make sure we are below the
1054 * interface MTU.
1055 * If the MTU wasn't set before use the interface mtu or
1056 * fall back to the next smaller mtu step compared to the
1057 * current packet size.
1058 */
1059 if (mtu != 0) {
1060 if (ia != NULL)
1061 mtu = min(mtu, ia->ia_ifp->if_mtu);
1062 } else {
1063 if (ia != NULL)
1064 mtu = ia->ia_ifp->if_mtu;
1065 else
1066 mtu = ip_next_mtu(ntohs(ip->ip_len), 0);
1067 }
1068 IPSTAT_INC(ips_cantfrag);
1069 break;
1070
1071 case ENOBUFS:
1072 case EACCES: /* ipfw denied packet */
1073 m_freem(mcopy);
1074 return;
1075 }
1076 icmp_error(mcopy, type, code, dest.s_addr, mtu);
1077 }
1078
1079 #define CHECK_SO_CT(sp, ct) \
1080 (((sp->so_options & SO_TIMESTAMP) && (sp->so_ts_clock == ct)) ? 1 : 0)
1081
1082 void
1083 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1084 struct mbuf *m)
1085 {
1086 bool stamped;
1087
1088 stamped = false;
1089 if ((inp->inp_socket->so_options & SO_BINTIME) ||
1090 CHECK_SO_CT(inp->inp_socket, SO_TS_BINTIME)) {
1091 struct bintime boottimebin, bt;
1092 struct timespec ts1;
1093
1094 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1095 M_TSTMP)) {
1096 mbuf_tstmp2timespec(m, &ts1);
1097 timespec2bintime(&ts1, &bt);
1098 getboottimebin(&boottimebin);
1099 bintime_add(&bt, &boottimebin);
1100 } else {
1101 bintime(&bt);
1102 }
1103 *mp = sbcreatecontrol(&bt, sizeof(bt), SCM_BINTIME,
1104 SOL_SOCKET, M_NOWAIT);
1105 if (*mp != NULL) {
1106 mp = &(*mp)->m_next;
1107 stamped = true;
1108 }
1109 }
1110 if (CHECK_SO_CT(inp->inp_socket, SO_TS_REALTIME_MICRO)) {
1111 struct bintime boottimebin, bt1;
1112 struct timespec ts1;
1113 struct timeval tv;
1114
1115 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1116 M_TSTMP)) {
1117 mbuf_tstmp2timespec(m, &ts1);
1118 timespec2bintime(&ts1, &bt1);
1119 getboottimebin(&boottimebin);
1120 bintime_add(&bt1, &boottimebin);
1121 bintime2timeval(&bt1, &tv);
1122 } else {
1123 microtime(&tv);
1124 }
1125 *mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv), SCM_TIMESTAMP,
1126 SOL_SOCKET, M_NOWAIT);
1127 if (*mp != NULL) {
1128 mp = &(*mp)->m_next;
1129 stamped = true;
1130 }
1131 } else if (CHECK_SO_CT(inp->inp_socket, SO_TS_REALTIME)) {
1132 struct bintime boottimebin;
1133 struct timespec ts, ts1;
1134
1135 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1136 M_TSTMP)) {
1137 mbuf_tstmp2timespec(m, &ts);
1138 getboottimebin(&boottimebin);
1139 bintime2timespec(&boottimebin, &ts1);
1140 timespecadd(&ts, &ts1, &ts);
1141 } else {
1142 nanotime(&ts);
1143 }
1144 *mp = sbcreatecontrol(&ts, sizeof(ts), SCM_REALTIME,
1145 SOL_SOCKET, M_NOWAIT);
1146 if (*mp != NULL) {
1147 mp = &(*mp)->m_next;
1148 stamped = true;
1149 }
1150 } else if (CHECK_SO_CT(inp->inp_socket, SO_TS_MONOTONIC)) {
1151 struct timespec ts;
1152
1153 if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1154 M_TSTMP))
1155 mbuf_tstmp2timespec(m, &ts);
1156 else
1157 nanouptime(&ts);
1158 *mp = sbcreatecontrol(&ts, sizeof(ts), SCM_MONOTONIC,
1159 SOL_SOCKET, M_NOWAIT);
1160 if (*mp != NULL) {
1161 mp = &(*mp)->m_next;
1162 stamped = true;
1163 }
1164 }
1165 if (stamped && (m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1166 M_TSTMP)) {
1167 struct sock_timestamp_info sti;
1168
1169 bzero(&sti, sizeof(sti));
1170 sti.st_info_flags = ST_INFO_HW;
1171 if ((m->m_flags & M_TSTMP_HPREC) != 0)
1172 sti.st_info_flags |= ST_INFO_HW_HPREC;
1173 *mp = sbcreatecontrol(&sti, sizeof(sti), SCM_TIME_INFO,
1174 SOL_SOCKET, M_NOWAIT);
1175 if (*mp != NULL)
1176 mp = &(*mp)->m_next;
1177 }
1178 if (inp->inp_flags & INP_RECVDSTADDR) {
1179 *mp = sbcreatecontrol(&ip->ip_dst, sizeof(struct in_addr),
1180 IP_RECVDSTADDR, IPPROTO_IP, M_NOWAIT);
1181 if (*mp)
1182 mp = &(*mp)->m_next;
1183 }
1184 if (inp->inp_flags & INP_RECVTTL) {
1185 *mp = sbcreatecontrol(&ip->ip_ttl, sizeof(u_char), IP_RECVTTL,
1186 IPPROTO_IP, M_NOWAIT);
1187 if (*mp)
1188 mp = &(*mp)->m_next;
1189 }
1190 #ifdef notyet
1191 /* XXX
1192 * Moving these out of udp_input() made them even more broken
1193 * than they already were.
1194 */
1195 /* options were tossed already */
1196 if (inp->inp_flags & INP_RECVOPTS) {
1197 *mp = sbcreatecontrol(opts_deleted_above,
1198 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP, M_NOWAIT);
1199 if (*mp)
1200 mp = &(*mp)->m_next;
1201 }
1202 /* ip_srcroute doesn't do what we want here, need to fix */
1203 if (inp->inp_flags & INP_RECVRETOPTS) {
1204 *mp = sbcreatecontrol(ip_srcroute(m), sizeof(struct in_addr),
1205 IP_RECVRETOPTS, IPPROTO_IP, M_NOWAIT);
1206 if (*mp)
1207 mp = &(*mp)->m_next;
1208 }
1209 #endif
1210 if (inp->inp_flags & INP_RECVIF) {
1211 struct ifnet *ifp;
1212 struct sdlbuf {
1213 struct sockaddr_dl sdl;
1214 u_char pad[32];
1215 } sdlbuf;
1216 struct sockaddr_dl *sdp;
1217 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1218
1219 if ((ifp = m->m_pkthdr.rcvif)) {
1220 sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1221 /*
1222 * Change our mind and don't try copy.
1223 */
1224 if (sdp->sdl_family != AF_LINK ||
1225 sdp->sdl_len > sizeof(sdlbuf)) {
1226 goto makedummy;
1227 }
1228 bcopy(sdp, sdl2, sdp->sdl_len);
1229 } else {
1230 makedummy:
1231 sdl2->sdl_len =
1232 offsetof(struct sockaddr_dl, sdl_data[0]);
1233 sdl2->sdl_family = AF_LINK;
1234 sdl2->sdl_index = 0;
1235 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1236 }
1237 *mp = sbcreatecontrol(sdl2, sdl2->sdl_len, IP_RECVIF,
1238 IPPROTO_IP, M_NOWAIT);
1239 if (*mp)
1240 mp = &(*mp)->m_next;
1241 }
1242 if (inp->inp_flags & INP_RECVTOS) {
1243 *mp = sbcreatecontrol(&ip->ip_tos, sizeof(u_char), IP_RECVTOS,
1244 IPPROTO_IP, M_NOWAIT);
1245 if (*mp)
1246 mp = &(*mp)->m_next;
1247 }
1248
1249 if (inp->inp_flags2 & INP_RECVFLOWID) {
1250 uint32_t flowid, flow_type;
1251
1252 flowid = m->m_pkthdr.flowid;
1253 flow_type = M_HASHTYPE_GET(m);
1254
1255 /*
1256 * XXX should handle the failure of one or the
1257 * other - don't populate both?
1258 */
1259 *mp = sbcreatecontrol(&flowid, sizeof(uint32_t), IP_FLOWID,
1260 IPPROTO_IP, M_NOWAIT);
1261 if (*mp)
1262 mp = &(*mp)->m_next;
1263 *mp = sbcreatecontrol(&flow_type, sizeof(uint32_t),
1264 IP_FLOWTYPE, IPPROTO_IP, M_NOWAIT);
1265 if (*mp)
1266 mp = &(*mp)->m_next;
1267 }
1268
1269 #ifdef RSS
1270 if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
1271 uint32_t flowid, flow_type;
1272 uint32_t rss_bucketid;
1273
1274 flowid = m->m_pkthdr.flowid;
1275 flow_type = M_HASHTYPE_GET(m);
1276
1277 if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1278 *mp = sbcreatecontrol(&rss_bucketid, sizeof(uint32_t),
1279 IP_RSSBUCKETID, IPPROTO_IP, M_NOWAIT);
1280 if (*mp)
1281 mp = &(*mp)->m_next;
1282 }
1283 }
1284 #endif
1285 }
1286
1287 /*
1288 * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
1289 * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
1290 * locking. This code remains in ip_input.c as ip_mroute.c is optionally
1291 * compiled.
1292 */
1293 VNET_DEFINE_STATIC(int, ip_rsvp_on);
1294 VNET_DEFINE(struct socket *, ip_rsvpd);
1295
1296 #define V_ip_rsvp_on VNET(ip_rsvp_on)
1297
1298 int
1299 ip_rsvp_init(struct socket *so)
1300 {
1301
1302 if (V_ip_rsvpd != NULL)
1303 return EADDRINUSE;
1304
1305 V_ip_rsvpd = so;
1306 /*
1307 * This may seem silly, but we need to be sure we don't over-increment
1308 * the RSVP counter, in case something slips up.
1309 */
1310 if (!V_ip_rsvp_on) {
1311 V_ip_rsvp_on = 1;
1312 V_rsvp_on++;
1313 }
1314
1315 return 0;
1316 }
1317
1318 int
1319 ip_rsvp_done(void)
1320 {
1321
1322 V_ip_rsvpd = NULL;
1323 /*
1324 * This may seem silly, but we need to be sure we don't over-decrement
1325 * the RSVP counter, in case something slips up.
1326 */
1327 if (V_ip_rsvp_on) {
1328 V_ip_rsvp_on = 0;
1329 V_rsvp_on--;
1330 }
1331 return 0;
1332 }
1333
1334 int
1335 rsvp_input(struct mbuf **mp, int *offp, int proto)
1336 {
1337 struct mbuf *m;
1338
1339 m = *mp;
1340 *mp = NULL;
1341
1342 if (rsvp_input_p) { /* call the real one if loaded */
1343 *mp = m;
1344 rsvp_input_p(mp, offp, proto);
1345 return (IPPROTO_DONE);
1346 }
1347
1348 /* Can still get packets with rsvp_on = 0 if there is a local member
1349 * of the group to which the RSVP packet is addressed. But in this
1350 * case we want to throw the packet away.
1351 */
1352
1353 if (!V_rsvp_on) {
1354 m_freem(m);
1355 return (IPPROTO_DONE);
1356 }
1357
1358 if (V_ip_rsvpd != NULL) {
1359 *mp = m;
1360 rip_input(mp, offp, proto);
1361 return (IPPROTO_DONE);
1362 }
1363 /* Drop the packet */
1364 m_freem(m);
1365 return (IPPROTO_DONE);
1366 }
Cache object: 3205072e9a047f868976f60983291d89
|