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