ip_input.c revision 1.311 1 /* $NetBSD: ip_input.c,v 1.311 2014/05/22 22:01:12 rmind Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * 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 project 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 PROJECT 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 PROJECT 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 /*-
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Public Access Networks Corporation ("Panix"). It was developed under
38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
60 */
61
62 /*
63 * Copyright (c) 1982, 1986, 1988, 1993
64 * The Regents of the University of California. All rights reserved.
65 *
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
68 * are met:
69 * 1. Redistributions of source code must retain the above copyright
70 * notice, this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright
72 * notice, this list of conditions and the following disclaimer in the
73 * documentation and/or other materials provided with the distribution.
74 * 3. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
77 *
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88 * SUCH DAMAGE.
89 *
90 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
91 */
92
93 #include <sys/cdefs.h>
94 __KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.311 2014/05/22 22:01:12 rmind Exp $");
95
96 #include "opt_inet.h"
97 #include "opt_compat_netbsd.h"
98 #include "opt_gateway.h"
99 #include "opt_ipsec.h"
100 #include "opt_mrouting.h"
101 #include "opt_mbuftrace.h"
102 #include "opt_inet_csum.h"
103
104 #include <sys/param.h>
105 #include <sys/systm.h>
106 #include <sys/mbuf.h>
107 #include <sys/domain.h>
108 #include <sys/protosw.h>
109 #include <sys/socket.h>
110 #include <sys/socketvar.h>
111 #include <sys/errno.h>
112 #include <sys/time.h>
113 #include <sys/kernel.h>
114 #include <sys/pool.h>
115 #include <sys/sysctl.h>
116 #include <sys/kauth.h>
117
118 #include <net/if.h>
119 #include <net/if_dl.h>
120 #include <net/route.h>
121 #include <net/pfil.h>
122
123 #include <netinet/in.h>
124 #include <netinet/in_systm.h>
125 #include <netinet/ip.h>
126 #include <netinet/in_pcb.h>
127 #include <netinet/in_proto.h>
128 #include <netinet/in_var.h>
129 #include <netinet/ip_var.h>
130 #include <netinet/ip_private.h>
131 #include <netinet/ip_icmp.h>
132 /* just for gif_ttl */
133 #include <netinet/in_gif.h>
134 #include "gif.h"
135 #include <net/if_gre.h>
136 #include "gre.h"
137
138 #ifdef MROUTING
139 #include <netinet/ip_mroute.h>
140 #endif
141 #include <netinet/portalgo.h>
142
143 #ifdef IPSEC
144 #include <netipsec/ipsec.h>
145 #endif
146
147 #ifndef IPFORWARDING
148 #ifdef GATEWAY
149 #define IPFORWARDING 1 /* forward IP packets not for us */
150 #else /* GATEWAY */
151 #define IPFORWARDING 0 /* don't forward IP packets not for us */
152 #endif /* GATEWAY */
153 #endif /* IPFORWARDING */
154 #ifndef IPSENDREDIRECTS
155 #define IPSENDREDIRECTS 1
156 #endif
157 #ifndef IPFORWSRCRT
158 #define IPFORWSRCRT 1 /* forward source-routed packets */
159 #endif
160 #ifndef IPALLOWSRCRT
161 #define IPALLOWSRCRT 1 /* allow source-routed packets */
162 #endif
163 #ifndef IPMTUDISC
164 #define IPMTUDISC 1
165 #endif
166 #ifndef IPMTUDISCTIMEOUT
167 #define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */
168 #endif
169
170 #ifdef COMPAT_50
171 #include <compat/sys/time.h>
172 #include <compat/sys/socket.h>
173 #endif
174
175 /*
176 * Note: DIRECTED_BROADCAST is handled this way so that previous
177 * configuration using this option will Just Work.
178 */
179 #ifndef IPDIRECTEDBCAST
180 #ifdef DIRECTED_BROADCAST
181 #define IPDIRECTEDBCAST 1
182 #else
183 #define IPDIRECTEDBCAST 0
184 #endif /* DIRECTED_BROADCAST */
185 #endif /* IPDIRECTEDBCAST */
186 int ipforwarding = IPFORWARDING;
187 int ipsendredirects = IPSENDREDIRECTS;
188 int ip_defttl = IPDEFTTL;
189 int ip_forwsrcrt = IPFORWSRCRT;
190 int ip_directedbcast = IPDIRECTEDBCAST;
191 int ip_allowsrcrt = IPALLOWSRCRT;
192 int ip_mtudisc = IPMTUDISC;
193 int ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
194 #ifdef DIAGNOSTIC
195 int ipprintfs = 0;
196 #endif
197
198 int ip_do_randomid = 0;
199
200 /*
201 * XXX - Setting ip_checkinterface mostly implements the receive side of
202 * the Strong ES model described in RFC 1122, but since the routing table
203 * and transmit implementation do not implement the Strong ES model,
204 * setting this to 1 results in an odd hybrid.
205 *
206 * XXX - ip_checkinterface currently must be disabled if you use ipnat
207 * to translate the destination address to another local interface.
208 *
209 * XXX - ip_checkinterface must be disabled if you add IP aliases
210 * to the loopback interface instead of the interface where the
211 * packets for those addresses are received.
212 */
213 int ip_checkinterface = 0;
214
215 struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
216
217 struct ifqueue ipintrq;
218
219 ipid_state_t * ip_ids;
220 uint16_t ip_id;
221
222 percpu_t *ipstat_percpu;
223
224 pfil_head_t *inet_pfil_hook;
225
226 #ifdef INET_CSUM_COUNTERS
227 #include <sys/device.h>
228
229 struct evcnt ip_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
230 NULL, "inet", "hwcsum bad");
231 struct evcnt ip_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
232 NULL, "inet", "hwcsum ok");
233 struct evcnt ip_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
234 NULL, "inet", "swcsum");
235
236 #define INET_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
237
238 EVCNT_ATTACH_STATIC(ip_hwcsum_bad);
239 EVCNT_ATTACH_STATIC(ip_hwcsum_ok);
240 EVCNT_ATTACH_STATIC(ip_swcsum);
241
242 #else
243
244 #define INET_CSUM_COUNTER_INCR(ev) /* nothing */
245
246 #endif /* INET_CSUM_COUNTERS */
247
248 /*
249 * We need to save the IP options in case a protocol wants to respond
250 * to an incoming packet over the same route if the packet got here
251 * using IP source routing. This allows connection establishment and
252 * maintenance when the remote end is on a network that is not known
253 * to us.
254 */
255
256 static int ip_nhops = 0;
257
258 static struct ip_srcrt {
259 struct in_addr dst; /* final destination */
260 char nop; /* one NOP to align */
261 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
262 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
263 } ip_srcrt;
264
265 static int ip_drainwanted;
266
267 static void save_rte(u_char *, struct in_addr);
268
269 #ifdef MBUFTRACE
270 struct mowner ip_rx_mowner = MOWNER_INIT("internet", "rx");
271 struct mowner ip_tx_mowner = MOWNER_INIT("internet", "tx");
272 #endif
273
274 static bool ip_dooptions(struct mbuf *);
275 static struct in_ifaddr *ip_rtaddr(struct in_addr);
276 static void sysctl_net_inet_ip_setup(struct sysctllog **);
277
278 /*
279 * IP initialization: fill in IP protocol switch table.
280 * All protocols not implemented in kernel go to raw IP protocol handler.
281 */
282 void
283 ip_init(void)
284 {
285 const struct protosw *pr;
286
287 in_init();
288 sysctl_net_inet_ip_setup(NULL);
289
290 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
291 KASSERT(pr != NULL);
292
293 for (u_int i = 0; i < IPPROTO_MAX; i++) {
294 ip_protox[i] = pr - inetsw;
295 }
296 for (pr = inetdomain.dom_protosw;
297 pr < inetdomain.dom_protoswNPROTOSW; pr++)
298 if (pr->pr_domain->dom_family == PF_INET &&
299 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
300 ip_protox[pr->pr_protocol] = pr - inetsw;
301
302 ip_reass_init();
303
304 ip_ids = ip_id_init();
305 ip_id = time_second & 0xfffff;
306
307 ipintrq.ifq_maxlen = IFQ_MAXLEN;
308
309 ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout);
310 #ifdef GATEWAY
311 ipflow_init();
312 #endif
313
314 /* Register our Packet Filter hook. */
315 inet_pfil_hook = pfil_head_create(PFIL_TYPE_AF, (void *)AF_INET);
316 KASSERT(inet_pfil_hook != NULL);
317
318 #ifdef MBUFTRACE
319 MOWNER_ATTACH(&ip_tx_mowner);
320 MOWNER_ATTACH(&ip_rx_mowner);
321 #endif /* MBUFTRACE */
322
323 ipstat_percpu = percpu_alloc(sizeof(uint64_t) * IP_NSTATS);
324 }
325
326 struct sockaddr_in ipaddr = {
327 .sin_len = sizeof(ipaddr),
328 .sin_family = AF_INET,
329 };
330
331 static struct route ipforward_rt;
332
333 /*
334 * IP software interrupt routine
335 */
336 void
337 ipintr(void)
338 {
339 int s;
340 struct mbuf *m;
341 struct ifqueue lcl_intrq;
342
343 memset(&lcl_intrq, 0, sizeof(lcl_intrq));
344
345 mutex_enter(softnet_lock);
346 KERNEL_LOCK(1, NULL);
347 if (!IF_IS_EMPTY(&ipintrq)) {
348 s = splnet();
349
350 /* Take existing queue onto stack */
351 lcl_intrq = ipintrq;
352
353 /* Zero out global queue, preserving maxlen and drops */
354 ipintrq.ifq_head = NULL;
355 ipintrq.ifq_tail = NULL;
356 ipintrq.ifq_len = 0;
357 ipintrq.ifq_maxlen = lcl_intrq.ifq_maxlen;
358 ipintrq.ifq_drops = lcl_intrq.ifq_drops;
359
360 splx(s);
361 }
362 KERNEL_UNLOCK_ONE(NULL);
363 while (!IF_IS_EMPTY(&lcl_intrq)) {
364 IF_DEQUEUE(&lcl_intrq, m);
365 if (m == NULL)
366 break;
367 ip_input(m);
368 }
369 mutex_exit(softnet_lock);
370 }
371
372 /*
373 * Ip input routine. Checksum and byte swap header. If fragmented
374 * try to reassemble. Process options. Pass to next level.
375 */
376 void
377 ip_input(struct mbuf *m)
378 {
379 struct ip *ip = NULL;
380 struct in_ifaddr *ia;
381 struct ifaddr *ifa;
382 int hlen = 0, len;
383 int downmatch;
384 int checkif;
385 int srcrt = 0;
386
387 MCLAIM(m, &ip_rx_mowner);
388 KASSERT((m->m_flags & M_PKTHDR) != 0);
389
390 /*
391 * If no IP addresses have been set yet but the interfaces
392 * are receiving, can't do anything with incoming packets yet.
393 */
394 if (TAILQ_FIRST(&in_ifaddrhead) == 0)
395 goto bad;
396 IP_STATINC(IP_STAT_TOTAL);
397 /*
398 * If the IP header is not aligned, slurp it up into a new
399 * mbuf with space for link headers, in the event we forward
400 * it. Otherwise, if it is aligned, make sure the entire
401 * base IP header is in the first mbuf of the chain.
402 */
403 if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
404 if ((m = m_copyup(m, sizeof(struct ip),
405 (max_linkhdr + 3) & ~3)) == NULL) {
406 /* XXXJRT new stat, please */
407 IP_STATINC(IP_STAT_TOOSMALL);
408 return;
409 }
410 } else if (__predict_false(m->m_len < sizeof (struct ip))) {
411 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
412 IP_STATINC(IP_STAT_TOOSMALL);
413 return;
414 }
415 }
416 ip = mtod(m, struct ip *);
417 if (ip->ip_v != IPVERSION) {
418 IP_STATINC(IP_STAT_BADVERS);
419 goto bad;
420 }
421 hlen = ip->ip_hl << 2;
422 if (hlen < sizeof(struct ip)) { /* minimum header length */
423 IP_STATINC(IP_STAT_BADHLEN);
424 goto bad;
425 }
426 if (hlen > m->m_len) {
427 if ((m = m_pullup(m, hlen)) == NULL) {
428 IP_STATINC(IP_STAT_BADHLEN);
429 return;
430 }
431 ip = mtod(m, struct ip *);
432 }
433
434 /*
435 * RFC1122: packets with a multicast source address are
436 * not allowed.
437 */
438 if (IN_MULTICAST(ip->ip_src.s_addr)) {
439 IP_STATINC(IP_STAT_BADADDR);
440 goto bad;
441 }
442
443 /* 127/8 must not appear on wire - RFC1122 */
444 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
445 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
446 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
447 IP_STATINC(IP_STAT_BADADDR);
448 goto bad;
449 }
450 }
451
452 switch (m->m_pkthdr.csum_flags &
453 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
454 M_CSUM_IPv4_BAD)) {
455 case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
456 INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad);
457 goto badcsum;
458
459 case M_CSUM_IPv4:
460 /* Checksum was okay. */
461 INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok);
462 break;
463
464 default:
465 /*
466 * Must compute it ourselves. Maybe skip checksum on
467 * loopback interfaces.
468 */
469 if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
470 IFF_LOOPBACK) || ip_do_loopback_cksum)) {
471 INET_CSUM_COUNTER_INCR(&ip_swcsum);
472 if (in_cksum(m, hlen) != 0)
473 goto badcsum;
474 }
475 break;
476 }
477
478 /* Retrieve the packet length. */
479 len = ntohs(ip->ip_len);
480
481 /*
482 * Check for additional length bogosity
483 */
484 if (len < hlen) {
485 IP_STATINC(IP_STAT_BADLEN);
486 goto bad;
487 }
488
489 /*
490 * Check that the amount of data in the buffers
491 * is as at least much as the IP header would have us expect.
492 * Trim mbufs if longer than we expect.
493 * Drop packet if shorter than we expect.
494 */
495 if (m->m_pkthdr.len < len) {
496 IP_STATINC(IP_STAT_TOOSHORT);
497 goto bad;
498 }
499 if (m->m_pkthdr.len > len) {
500 if (m->m_len == m->m_pkthdr.len) {
501 m->m_len = len;
502 m->m_pkthdr.len = len;
503 } else
504 m_adj(m, len - m->m_pkthdr.len);
505 }
506
507 /*
508 * Assume that we can create a fast-forward IP flow entry
509 * based on this packet.
510 */
511 m->m_flags |= M_CANFASTFWD;
512
513 /*
514 * Run through list of hooks for input packets. If there are any
515 * filters which require that additional packets in the flow are
516 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
517 * Note that filters must _never_ set this flag, as another filter
518 * in the list may have previously cleared it.
519 */
520 /*
521 * let ipfilter look at packet on the wire,
522 * not the decapsulated packet.
523 */
524 #if defined(IPSEC)
525 if (!ipsec_indone(m))
526 #else
527 if (1)
528 #endif
529 {
530 struct in_addr odst;
531
532 odst = ip->ip_dst;
533 if (pfil_run_hooks(inet_pfil_hook, &m, m->m_pkthdr.rcvif,
534 PFIL_IN) != 0)
535 return;
536 if (m == NULL)
537 return;
538 ip = mtod(m, struct ip *);
539 hlen = ip->ip_hl << 2;
540 /*
541 * XXX The setting of "srcrt" here is to prevent ip_forward()
542 * from generating ICMP redirects for packets that have
543 * been redirected by a hook back out on to the same LAN that
544 * they came from and is not an indication that the packet
545 * is being inffluenced by source routing options. This
546 * allows things like
547 * "rdr tlp0 0/0 port 80 -> 1.1.1.200 3128 tcp"
548 * where tlp0 is both on the 1.1.1.0/24 network and is the
549 * default route for hosts on 1.1.1.0/24. Of course this
550 * also requires a "map tlp0 ..." to complete the story.
551 * One might argue whether or not this kind of network config.
552 * should be supported in this manner...
553 */
554 srcrt = (odst.s_addr != ip->ip_dst.s_addr);
555 }
556
557 #ifdef ALTQ
558 /* XXX Temporary until ALTQ is changed to use a pfil hook */
559 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) {
560 /* packet dropped by traffic conditioner */
561 return;
562 }
563 #endif
564
565 /*
566 * Process options and, if not destined for us,
567 * ship it on. ip_dooptions returns 1 when an
568 * error was detected (causing an icmp message
569 * to be sent and the original packet to be freed).
570 */
571 ip_nhops = 0; /* for source routed packets */
572 if (hlen > sizeof (struct ip) && ip_dooptions(m))
573 return;
574
575 /*
576 * Enable a consistency check between the destination address
577 * and the arrival interface for a unicast packet (the RFC 1122
578 * strong ES model) if IP forwarding is disabled and the packet
579 * is not locally generated.
580 *
581 * XXX - Checking also should be disabled if the destination
582 * address is ipnat'ed to a different interface.
583 *
584 * XXX - Checking is incompatible with IP aliases added
585 * to the loopback interface instead of the interface where
586 * the packets are received.
587 *
588 * XXX - We need to add a per ifaddr flag for this so that
589 * we get finer grain control.
590 */
591 checkif = ip_checkinterface && (ipforwarding == 0) &&
592 (m->m_pkthdr.rcvif != NULL) &&
593 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0);
594
595 /*
596 * Check our list of addresses, to see if the packet is for us.
597 *
598 * Traditional 4.4BSD did not consult IFF_UP at all.
599 * The behavior here is to treat addresses on !IFF_UP interface
600 * as not mine.
601 */
602 downmatch = 0;
603 LIST_FOREACH(ia, &IN_IFADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
604 if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) {
605 if (checkif && ia->ia_ifp != m->m_pkthdr.rcvif)
606 continue;
607 if ((ia->ia_ifp->if_flags & IFF_UP) != 0)
608 break;
609 else
610 downmatch++;
611 }
612 }
613 if (ia != NULL)
614 goto ours;
615 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
616 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
617 if (ifa->ifa_addr->sa_family != AF_INET)
618 continue;
619 ia = ifatoia(ifa);
620 if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
621 in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
622 /*
623 * Look for all-0's host part (old broadcast addr),
624 * either for subnet or net.
625 */
626 ip->ip_dst.s_addr == ia->ia_subnet ||
627 ip->ip_dst.s_addr == ia->ia_net)
628 goto ours;
629 /*
630 * An interface with IP address zero accepts
631 * all packets that arrive on that interface.
632 */
633 if (in_nullhost(ia->ia_addr.sin_addr))
634 goto ours;
635 }
636 }
637 if (IN_MULTICAST(ip->ip_dst.s_addr)) {
638 struct in_multi *inm;
639 #ifdef MROUTING
640 extern struct socket *ip_mrouter;
641
642 if (ip_mrouter) {
643 /*
644 * If we are acting as a multicast router, all
645 * incoming multicast packets are passed to the
646 * kernel-level multicast forwarding function.
647 * The packet is returned (relatively) intact; if
648 * ip_mforward() returns a non-zero value, the packet
649 * must be discarded, else it may be accepted below.
650 *
651 * (The IP ident field is put in the same byte order
652 * as expected when ip_mforward() is called from
653 * ip_output().)
654 */
655 if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
656 IP_STATINC(IP_STAT_CANTFORWARD);
657 m_freem(m);
658 return;
659 }
660
661 /*
662 * The process-level routing demon needs to receive
663 * all multicast IGMP packets, whether or not this
664 * host belongs to their destination groups.
665 */
666 if (ip->ip_p == IPPROTO_IGMP)
667 goto ours;
668 IP_STATINC(IP_STAT_CANTFORWARD);
669 }
670 #endif
671 /*
672 * See if we belong to the destination multicast group on the
673 * arrival interface.
674 */
675 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
676 if (inm == NULL) {
677 IP_STATINC(IP_STAT_CANTFORWARD);
678 m_freem(m);
679 return;
680 }
681 goto ours;
682 }
683 if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
684 in_nullhost(ip->ip_dst))
685 goto ours;
686
687 /*
688 * Not for us; forward if possible and desirable.
689 */
690 if (ipforwarding == 0) {
691 IP_STATINC(IP_STAT_CANTFORWARD);
692 m_freem(m);
693 } else {
694 /*
695 * If ip_dst matched any of my address on !IFF_UP interface,
696 * and there's no IFF_UP interface that matches ip_dst,
697 * send icmp unreach. Forwarding it will result in in-kernel
698 * forwarding loop till TTL goes to 0.
699 */
700 if (downmatch) {
701 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
702 IP_STATINC(IP_STAT_CANTFORWARD);
703 return;
704 }
705 #ifdef IPSEC
706 /* Perform IPsec, if any. */
707 if (ipsec4_input(m, IP_FORWARDING | (ip_directedbcast ?
708 IP_ALLOWBROADCAST : 0)) != 0) {
709 goto bad;
710 }
711 #endif
712 ip_forward(m, srcrt);
713 }
714 return;
715
716 ours:
717 /*
718 * If offset or IP_MF are set, must reassemble.
719 */
720 if (ip->ip_off & ~htons(IP_DF|IP_RF)) {
721 /*
722 * Pass to IP reassembly mechanism.
723 */
724 if (ip_reass_packet(&m, ip) != 0) {
725 /* Failed; invalid fragment(s) or packet. */
726 goto bad;
727 }
728 if (m == NULL) {
729 /* More fragments should come; silently return. */
730 return;
731 }
732 /*
733 * Reassembly is done, we have the final packet.
734 * Updated cached data in local variable(s).
735 */
736 ip = mtod(m, struct ip *);
737 hlen = ip->ip_hl << 2;
738 }
739
740 #ifdef IPSEC
741 /*
742 * Enforce IPsec policy checking if we are seeing last header.
743 * Note that we do not visit this with protocols with PCB layer
744 * code - like UDP/TCP/raw IP.
745 */
746 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
747 if (ipsec4_input(m, 0) != 0) {
748 goto bad;
749 }
750 }
751 #endif
752
753 /*
754 * Switch out to protocol's input routine.
755 */
756 #if IFA_STATS
757 if (ia && ip)
758 ia->ia_ifa.ifa_data.ifad_inbytes += ntohs(ip->ip_len);
759 #endif
760 IP_STATINC(IP_STAT_DELIVERED);
761 {
762 int off = hlen, nh = ip->ip_p;
763
764 (*inetsw[ip_protox[nh]].pr_input)(m, off, nh);
765 return;
766 }
767 bad:
768 m_freem(m);
769 return;
770
771 badcsum:
772 IP_STATINC(IP_STAT_BADSUM);
773 m_freem(m);
774 }
775
776 /*
777 * IP timer processing.
778 */
779 void
780 ip_slowtimo(void)
781 {
782
783 mutex_enter(softnet_lock);
784 KERNEL_LOCK(1, NULL);
785
786 ip_reass_slowtimo();
787
788 KERNEL_UNLOCK_ONE(NULL);
789 mutex_exit(softnet_lock);
790 }
791
792 /*
793 * IP drain processing.
794 */
795 void
796 ip_drain(void)
797 {
798
799 KERNEL_LOCK(1, NULL);
800 ip_reass_drain();
801 KERNEL_UNLOCK_ONE(NULL);
802 }
803
804 /*
805 * ip_dooptions: perform option processing on a datagram, possibly discarding
806 * it if bad options are encountered, or forwarding it if source-routed.
807 *
808 * => Returns true if packet has been forwarded/freed.
809 * => Returns false if the packet should be processed further.
810 */
811 static bool
812 ip_dooptions(struct mbuf *m)
813 {
814 struct ip *ip = mtod(m, struct ip *);
815 u_char *cp, *cp0;
816 struct ip_timestamp *ipt;
817 struct in_ifaddr *ia;
818 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
819 struct in_addr dst;
820 n_time ntime;
821
822 dst = ip->ip_dst;
823 cp = (u_char *)(ip + 1);
824 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
825 for (; cnt > 0; cnt -= optlen, cp += optlen) {
826 opt = cp[IPOPT_OPTVAL];
827 if (opt == IPOPT_EOL)
828 break;
829 if (opt == IPOPT_NOP)
830 optlen = 1;
831 else {
832 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
833 code = &cp[IPOPT_OLEN] - (u_char *)ip;
834 goto bad;
835 }
836 optlen = cp[IPOPT_OLEN];
837 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
838 code = &cp[IPOPT_OLEN] - (u_char *)ip;
839 goto bad;
840 }
841 }
842 switch (opt) {
843
844 default:
845 break;
846
847 /*
848 * Source routing with record.
849 * Find interface with current destination address.
850 * If none on this machine then drop if strictly routed,
851 * or do nothing if loosely routed.
852 * Record interface address and bring up next address
853 * component. If strictly routed make sure next
854 * address is on directly accessible net.
855 */
856 case IPOPT_LSRR:
857 case IPOPT_SSRR:
858 if (ip_allowsrcrt == 0) {
859 type = ICMP_UNREACH;
860 code = ICMP_UNREACH_NET_PROHIB;
861 goto bad;
862 }
863 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
864 code = &cp[IPOPT_OLEN] - (u_char *)ip;
865 goto bad;
866 }
867 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
868 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
869 goto bad;
870 }
871 ipaddr.sin_addr = ip->ip_dst;
872 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
873 if (ia == 0) {
874 if (opt == IPOPT_SSRR) {
875 type = ICMP_UNREACH;
876 code = ICMP_UNREACH_SRCFAIL;
877 goto bad;
878 }
879 /*
880 * Loose routing, and not at next destination
881 * yet; nothing to do except forward.
882 */
883 break;
884 }
885 off--; /* 0 origin */
886 if ((off + sizeof(struct in_addr)) > optlen) {
887 /*
888 * End of source route. Should be for us.
889 */
890 save_rte(cp, ip->ip_src);
891 break;
892 }
893 /*
894 * locate outgoing interface
895 */
896 memcpy((void *)&ipaddr.sin_addr, (void *)(cp + off),
897 sizeof(ipaddr.sin_addr));
898 if (opt == IPOPT_SSRR)
899 ia = ifatoia(ifa_ifwithladdr(sintosa(&ipaddr)));
900 else
901 ia = ip_rtaddr(ipaddr.sin_addr);
902 if (ia == 0) {
903 type = ICMP_UNREACH;
904 code = ICMP_UNREACH_SRCFAIL;
905 goto bad;
906 }
907 ip->ip_dst = ipaddr.sin_addr;
908 bcopy((void *)&ia->ia_addr.sin_addr,
909 (void *)(cp + off), sizeof(struct in_addr));
910 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
911 /*
912 * Let ip_intr's mcast routing check handle mcast pkts
913 */
914 forward = !IN_MULTICAST(ip->ip_dst.s_addr);
915 break;
916
917 case IPOPT_RR:
918 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
919 code = &cp[IPOPT_OLEN] - (u_char *)ip;
920 goto bad;
921 }
922 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
923 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
924 goto bad;
925 }
926 /*
927 * If no space remains, ignore.
928 */
929 off--; /* 0 origin */
930 if ((off + sizeof(struct in_addr)) > optlen)
931 break;
932 memcpy((void *)&ipaddr.sin_addr, (void *)(&ip->ip_dst),
933 sizeof(ipaddr.sin_addr));
934 /*
935 * locate outgoing interface; if we're the destination,
936 * use the incoming interface (should be same).
937 */
938 if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))))
939 == NULL &&
940 (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
941 type = ICMP_UNREACH;
942 code = ICMP_UNREACH_HOST;
943 goto bad;
944 }
945 bcopy((void *)&ia->ia_addr.sin_addr,
946 (void *)(cp + off), sizeof(struct in_addr));
947 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
948 break;
949
950 case IPOPT_TS:
951 code = cp - (u_char *)ip;
952 ipt = (struct ip_timestamp *)cp;
953 if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
954 code = (u_char *)&ipt->ipt_len - (u_char *)ip;
955 goto bad;
956 }
957 if (ipt->ipt_ptr < 5) {
958 code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
959 goto bad;
960 }
961 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
962 if (++ipt->ipt_oflw == 0) {
963 code = (u_char *)&ipt->ipt_ptr -
964 (u_char *)ip;
965 goto bad;
966 }
967 break;
968 }
969 cp0 = (cp + ipt->ipt_ptr - 1);
970 switch (ipt->ipt_flg) {
971
972 case IPOPT_TS_TSONLY:
973 break;
974
975 case IPOPT_TS_TSANDADDR:
976 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
977 sizeof(struct in_addr) > ipt->ipt_len) {
978 code = (u_char *)&ipt->ipt_ptr -
979 (u_char *)ip;
980 goto bad;
981 }
982 ipaddr.sin_addr = dst;
983 ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
984 m->m_pkthdr.rcvif));
985 if (ia == 0)
986 continue;
987 bcopy(&ia->ia_addr.sin_addr,
988 cp0, sizeof(struct in_addr));
989 ipt->ipt_ptr += sizeof(struct in_addr);
990 break;
991
992 case IPOPT_TS_PRESPEC:
993 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
994 sizeof(struct in_addr) > ipt->ipt_len) {
995 code = (u_char *)&ipt->ipt_ptr -
996 (u_char *)ip;
997 goto bad;
998 }
999 memcpy(&ipaddr.sin_addr, cp0,
1000 sizeof(struct in_addr));
1001 if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))
1002 == NULL)
1003 continue;
1004 ipt->ipt_ptr += sizeof(struct in_addr);
1005 break;
1006
1007 default:
1008 /* XXX can't take &ipt->ipt_flg */
1009 code = (u_char *)&ipt->ipt_ptr -
1010 (u_char *)ip + 1;
1011 goto bad;
1012 }
1013 ntime = iptime();
1014 cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */
1015 memmove((char *)cp + ipt->ipt_ptr - 1, cp0,
1016 sizeof(n_time));
1017 ipt->ipt_ptr += sizeof(n_time);
1018 }
1019 }
1020 if (forward) {
1021 if (ip_forwsrcrt == 0) {
1022 type = ICMP_UNREACH;
1023 code = ICMP_UNREACH_SRCFAIL;
1024 goto bad;
1025 }
1026 ip_forward(m, 1);
1027 return true;
1028 }
1029 return false;
1030 bad:
1031 icmp_error(m, type, code, 0, 0);
1032 IP_STATINC(IP_STAT_BADOPTIONS);
1033 return true;
1034 }
1035
1036 /*
1037 * ip_rtaddr: given address of next destination (final or next hop),
1038 * return internet address info of interface to be used to get there.
1039 */
1040 static struct in_ifaddr *
1041 ip_rtaddr(struct in_addr dst)
1042 {
1043 struct rtentry *rt;
1044 union {
1045 struct sockaddr dst;
1046 struct sockaddr_in dst4;
1047 } u;
1048
1049 sockaddr_in_init(&u.dst4, &dst, 0);
1050
1051 if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL)
1052 return NULL;
1053
1054 return ifatoia(rt->rt_ifa);
1055 }
1056
1057 /*
1058 * save_rte: save incoming source route for use in replies, to be picked
1059 * up later by ip_srcroute if the receiver is interested.
1060 */
1061 static void
1062 save_rte(u_char *option, struct in_addr dst)
1063 {
1064 unsigned olen;
1065
1066 olen = option[IPOPT_OLEN];
1067 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1068 return;
1069 memcpy((void *)ip_srcrt.srcopt, (void *)option, olen);
1070 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1071 ip_srcrt.dst = dst;
1072 }
1073
1074 /*
1075 * Retrieve incoming source route for use in replies,
1076 * in the same form used by setsockopt.
1077 * The first hop is placed before the options, will be removed later.
1078 */
1079 struct mbuf *
1080 ip_srcroute(void)
1081 {
1082 struct in_addr *p, *q;
1083 struct mbuf *m;
1084
1085 if (ip_nhops == 0)
1086 return NULL;
1087 m = m_get(M_DONTWAIT, MT_SOOPTS);
1088 if (m == 0)
1089 return NULL;
1090
1091 MCLAIM(m, &inetdomain.dom_mowner);
1092 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1093
1094 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1095 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1096 OPTSIZ;
1097
1098 /*
1099 * First save first hop for return route
1100 */
1101 p = &ip_srcrt.route[ip_nhops - 1];
1102 *(mtod(m, struct in_addr *)) = *p--;
1103
1104 /*
1105 * Copy option fields and padding (nop) to mbuf.
1106 */
1107 ip_srcrt.nop = IPOPT_NOP;
1108 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1109 memmove(mtod(m, char *) + sizeof(struct in_addr), &ip_srcrt.nop,
1110 OPTSIZ);
1111 q = (struct in_addr *)(mtod(m, char *) +
1112 sizeof(struct in_addr) + OPTSIZ);
1113 #undef OPTSIZ
1114 /*
1115 * Record return path as an IP source route,
1116 * reversing the path (pointers are now aligned).
1117 */
1118 while (p >= ip_srcrt.route) {
1119 *q++ = *p--;
1120 }
1121 /*
1122 * Last hop goes to final destination.
1123 */
1124 *q = ip_srcrt.dst;
1125 return (m);
1126 }
1127
1128 const int inetctlerrmap[PRC_NCMDS] = {
1129 [PRC_MSGSIZE] = EMSGSIZE,
1130 [PRC_HOSTDEAD] = EHOSTDOWN,
1131 [PRC_HOSTUNREACH] = EHOSTUNREACH,
1132 [PRC_UNREACH_NET] = EHOSTUNREACH,
1133 [PRC_UNREACH_HOST] = EHOSTUNREACH,
1134 [PRC_UNREACH_PROTOCOL] = ECONNREFUSED,
1135 [PRC_UNREACH_PORT] = ECONNREFUSED,
1136 [PRC_UNREACH_SRCFAIL] = EHOSTUNREACH,
1137 [PRC_PARAMPROB] = ENOPROTOOPT,
1138 };
1139
1140 void
1141 ip_fasttimo(void)
1142 {
1143 if (ip_drainwanted) {
1144 ip_drain();
1145 ip_drainwanted = 0;
1146 }
1147 }
1148
1149 void
1150 ip_drainstub(void)
1151 {
1152 ip_drainwanted = 1;
1153 }
1154
1155 /*
1156 * Forward a packet. If some error occurs return the sender
1157 * an icmp packet. Note we can't always generate a meaningful
1158 * icmp message because icmp doesn't have a large enough repertoire
1159 * of codes and types.
1160 *
1161 * If not forwarding, just drop the packet. This could be confusing
1162 * if ipforwarding was zero but some routing protocol was advancing
1163 * us as a gateway to somewhere. However, we must let the routing
1164 * protocol deal with that.
1165 *
1166 * The srcrt parameter indicates whether the packet is being forwarded
1167 * via a source route.
1168 */
1169 void
1170 ip_forward(struct mbuf *m, int srcrt)
1171 {
1172 struct ip *ip = mtod(m, struct ip *);
1173 struct rtentry *rt;
1174 int error, type = 0, code = 0, destmtu = 0;
1175 struct mbuf *mcopy;
1176 n_long dest;
1177 union {
1178 struct sockaddr dst;
1179 struct sockaddr_in dst4;
1180 } u;
1181
1182 /*
1183 * We are now in the output path.
1184 */
1185 MCLAIM(m, &ip_tx_mowner);
1186
1187 /*
1188 * Clear any in-bound checksum flags for this packet.
1189 */
1190 m->m_pkthdr.csum_flags = 0;
1191
1192 dest = 0;
1193 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1194 IP_STATINC(IP_STAT_CANTFORWARD);
1195 m_freem(m);
1196 return;
1197 }
1198 if (ip->ip_ttl <= IPTTLDEC) {
1199 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1200 return;
1201 }
1202
1203 sockaddr_in_init(&u.dst4, &ip->ip_dst, 0);
1204 if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL) {
1205 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, dest, 0);
1206 return;
1207 }
1208
1209 /*
1210 * Save at most 68 bytes of the packet in case
1211 * we need to generate an ICMP message to the src.
1212 * Pullup to avoid sharing mbuf cluster between m and mcopy.
1213 */
1214 mcopy = m_copym(m, 0, imin(ntohs(ip->ip_len), 68), M_DONTWAIT);
1215 if (mcopy)
1216 mcopy = m_pullup(mcopy, ip->ip_hl << 2);
1217
1218 ip->ip_ttl -= IPTTLDEC;
1219
1220 /*
1221 * If forwarding packet using same interface that it came in on,
1222 * perhaps should send a redirect to sender to shortcut a hop.
1223 * Only send redirect if source is sending directly to us,
1224 * and if packet was not source routed (or has any options).
1225 * Also, don't send redirect if forwarding using a default route
1226 * or a route modified by a redirect.
1227 */
1228 if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1229 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1230 !in_nullhost(satocsin(rt_getkey(rt))->sin_addr) &&
1231 ipsendredirects && !srcrt) {
1232 if (rt->rt_ifa &&
1233 (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
1234 ifatoia(rt->rt_ifa)->ia_subnet) {
1235 if (rt->rt_flags & RTF_GATEWAY)
1236 dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1237 else
1238 dest = ip->ip_dst.s_addr;
1239 /*
1240 * Router requirements says to only send host
1241 * redirects.
1242 */
1243 type = ICMP_REDIRECT;
1244 code = ICMP_REDIRECT_HOST;
1245 }
1246 }
1247
1248 error = ip_output(m, NULL, &ipforward_rt,
1249 (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
1250 NULL, NULL);
1251
1252 if (error)
1253 IP_STATINC(IP_STAT_CANTFORWARD);
1254 else {
1255 uint64_t *ips = IP_STAT_GETREF();
1256 ips[IP_STAT_FORWARD]++;
1257 if (type) {
1258 ips[IP_STAT_REDIRECTSENT]++;
1259 IP_STAT_PUTREF();
1260 } else {
1261 IP_STAT_PUTREF();
1262 if (mcopy) {
1263 #ifdef GATEWAY
1264 if (mcopy->m_flags & M_CANFASTFWD)
1265 ipflow_create(&ipforward_rt, mcopy);
1266 #endif
1267 m_freem(mcopy);
1268 }
1269 return;
1270 }
1271 }
1272 if (mcopy == NULL)
1273 return;
1274
1275 switch (error) {
1276
1277 case 0: /* forwarded, but need redirect */
1278 /* type, code set above */
1279 break;
1280
1281 case ENETUNREACH: /* shouldn't happen, checked above */
1282 case EHOSTUNREACH:
1283 case ENETDOWN:
1284 case EHOSTDOWN:
1285 default:
1286 type = ICMP_UNREACH;
1287 code = ICMP_UNREACH_HOST;
1288 break;
1289
1290 case EMSGSIZE:
1291 type = ICMP_UNREACH;
1292 code = ICMP_UNREACH_NEEDFRAG;
1293
1294 if ((rt = rtcache_validate(&ipforward_rt)) != NULL)
1295 destmtu = rt->rt_ifp->if_mtu;
1296 #ifdef IPSEC
1297 (void)ipsec4_forward(mcopy, &destmtu);
1298 #endif
1299 IP_STATINC(IP_STAT_CANTFRAG);
1300 break;
1301
1302 case ENOBUFS:
1303 /*
1304 * Do not generate ICMP_SOURCEQUENCH as required in RFC 1812,
1305 * Requirements for IP Version 4 Routers. Source quench can
1306 * big problem under DoS attacks or if the underlying
1307 * interface is rate-limited.
1308 */
1309 if (mcopy)
1310 m_freem(mcopy);
1311 return;
1312 }
1313 icmp_error(mcopy, type, code, dest, destmtu);
1314 }
1315
1316 void
1317 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1318 struct mbuf *m)
1319 {
1320 struct socket *so = inp->inp_socket;
1321 int inpflags = inp->inp_flags;
1322
1323 if (so->so_options & SO_TIMESTAMP
1324 #ifdef SO_OTIMESTAMP
1325 || so->so_options & SO_OTIMESTAMP
1326 #endif
1327 ) {
1328 struct timeval tv;
1329
1330 microtime(&tv);
1331 #ifdef SO_OTIMESTAMP
1332 if (so->so_options & SO_OTIMESTAMP) {
1333 struct timeval50 tv50;
1334 timeval_to_timeval50(&tv, &tv50);
1335 *mp = sbcreatecontrol((void *) &tv50, sizeof(tv50),
1336 SCM_OTIMESTAMP, SOL_SOCKET);
1337 } else
1338 #endif
1339 *mp = sbcreatecontrol((void *) &tv, sizeof(tv),
1340 SCM_TIMESTAMP, SOL_SOCKET);
1341 if (*mp)
1342 mp = &(*mp)->m_next;
1343 }
1344 if (inpflags & INP_RECVDSTADDR) {
1345 *mp = sbcreatecontrol((void *) &ip->ip_dst,
1346 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1347 if (*mp)
1348 mp = &(*mp)->m_next;
1349 }
1350 if (inpflags & INP_RECVPKTINFO) {
1351 struct in_pktinfo ipi;
1352 ipi.ipi_addr = ip->ip_src;
1353 ipi.ipi_ifindex = m->m_pkthdr.rcvif->if_index;
1354 *mp = sbcreatecontrol((void *) &ipi,
1355 sizeof(ipi), IP_RECVPKTINFO, IPPROTO_IP);
1356 if (*mp)
1357 mp = &(*mp)->m_next;
1358 }
1359 if (inpflags & INP_PKTINFO) {
1360 struct in_pktinfo ipi;
1361 ipi.ipi_addr = ip->ip_dst;
1362 ipi.ipi_ifindex = m->m_pkthdr.rcvif->if_index;
1363 *mp = sbcreatecontrol((void *) &ipi,
1364 sizeof(ipi), IP_PKTINFO, IPPROTO_IP);
1365 if (*mp)
1366 mp = &(*mp)->m_next;
1367 }
1368 if (inpflags & INP_RECVIF) {
1369 struct sockaddr_dl sdl;
1370
1371 sockaddr_dl_init(&sdl, sizeof(sdl), m->m_pkthdr.rcvif ?
1372 m->m_pkthdr.rcvif->if_index : 0, 0, NULL, 0, NULL, 0);
1373 *mp = sbcreatecontrol(&sdl, sdl.sdl_len, IP_RECVIF, IPPROTO_IP);
1374 if (*mp)
1375 mp = &(*mp)->m_next;
1376 }
1377 if (inpflags & INP_RECVTTL) {
1378 *mp = sbcreatecontrol((void *) &ip->ip_ttl,
1379 sizeof(uint8_t), IP_RECVTTL, IPPROTO_IP);
1380 if (*mp)
1381 mp = &(*mp)->m_next;
1382 }
1383 }
1384
1385 /*
1386 * sysctl helper routine for net.inet.ip.forwsrcrt.
1387 */
1388 static int
1389 sysctl_net_inet_ip_forwsrcrt(SYSCTLFN_ARGS)
1390 {
1391 int error, tmp;
1392 struct sysctlnode node;
1393
1394 node = *rnode;
1395 tmp = ip_forwsrcrt;
1396 node.sysctl_data = &tmp;
1397 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1398 if (error || newp == NULL)
1399 return (error);
1400
1401 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FORWSRCRT,
1402 0, NULL, NULL, NULL);
1403 if (error)
1404 return (error);
1405
1406 ip_forwsrcrt = tmp;
1407
1408 return (0);
1409 }
1410
1411 /*
1412 * sysctl helper routine for net.inet.ip.mtudisctimeout. checks the
1413 * range of the new value and tweaks timers if it changes.
1414 */
1415 static int
1416 sysctl_net_inet_ip_pmtudto(SYSCTLFN_ARGS)
1417 {
1418 int error, tmp;
1419 struct sysctlnode node;
1420
1421 node = *rnode;
1422 tmp = ip_mtudisc_timeout;
1423 node.sysctl_data = &tmp;
1424 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1425 if (error || newp == NULL)
1426 return (error);
1427 if (tmp < 0)
1428 return (EINVAL);
1429
1430 mutex_enter(softnet_lock);
1431
1432 ip_mtudisc_timeout = tmp;
1433 rt_timer_queue_change(ip_mtudisc_timeout_q, ip_mtudisc_timeout);
1434
1435 mutex_exit(softnet_lock);
1436
1437 return (0);
1438 }
1439
1440 static int
1441 sysctl_net_inet_ip_stats(SYSCTLFN_ARGS)
1442 {
1443
1444 return (NETSTAT_SYSCTL(ipstat_percpu, IP_NSTATS));
1445 }
1446
1447 static void
1448 sysctl_net_inet_ip_setup(struct sysctllog **clog)
1449 {
1450 sysctl_createv(clog, 0, NULL, NULL,
1451 CTLFLAG_PERMANENT,
1452 CTLTYPE_NODE, "inet",
1453 SYSCTL_DESCR("PF_INET related settings"),
1454 NULL, 0, NULL, 0,
1455 CTL_NET, PF_INET, CTL_EOL);
1456 sysctl_createv(clog, 0, NULL, NULL,
1457 CTLFLAG_PERMANENT,
1458 CTLTYPE_NODE, "ip",
1459 SYSCTL_DESCR("IPv4 related settings"),
1460 NULL, 0, NULL, 0,
1461 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
1462
1463 sysctl_createv(clog, 0, NULL, NULL,
1464 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1465 CTLTYPE_INT, "forwarding",
1466 SYSCTL_DESCR("Enable forwarding of INET datagrams"),
1467 NULL, 0, &ipforwarding, 0,
1468 CTL_NET, PF_INET, IPPROTO_IP,
1469 IPCTL_FORWARDING, CTL_EOL);
1470 sysctl_createv(clog, 0, NULL, NULL,
1471 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1472 CTLTYPE_INT, "redirect",
1473 SYSCTL_DESCR("Enable sending of ICMP redirect messages"),
1474 NULL, 0, &ipsendredirects, 0,
1475 CTL_NET, PF_INET, IPPROTO_IP,
1476 IPCTL_SENDREDIRECTS, CTL_EOL);
1477 sysctl_createv(clog, 0, NULL, NULL,
1478 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1479 CTLTYPE_INT, "ttl",
1480 SYSCTL_DESCR("Default TTL for an INET datagram"),
1481 NULL, 0, &ip_defttl, 0,
1482 CTL_NET, PF_INET, IPPROTO_IP,
1483 IPCTL_DEFTTL, CTL_EOL);
1484 #ifdef IPCTL_DEFMTU
1485 sysctl_createv(clog, 0, NULL, NULL,
1486 CTLFLAG_PERMANENT /* |CTLFLAG_READWRITE? */,
1487 CTLTYPE_INT, "mtu",
1488 SYSCTL_DESCR("Default MTA for an INET route"),
1489 NULL, 0, &ip_mtu, 0,
1490 CTL_NET, PF_INET, IPPROTO_IP,
1491 IPCTL_DEFMTU, CTL_EOL);
1492 #endif /* IPCTL_DEFMTU */
1493 sysctl_createv(clog, 0, NULL, NULL,
1494 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1495 CTLTYPE_INT, "forwsrcrt",
1496 SYSCTL_DESCR("Enable forwarding of source-routed "
1497 "datagrams"),
1498 sysctl_net_inet_ip_forwsrcrt, 0, &ip_forwsrcrt, 0,
1499 CTL_NET, PF_INET, IPPROTO_IP,
1500 IPCTL_FORWSRCRT, CTL_EOL);
1501 sysctl_createv(clog, 0, NULL, NULL,
1502 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1503 CTLTYPE_INT, "directed-broadcast",
1504 SYSCTL_DESCR("Enable forwarding of broadcast datagrams"),
1505 NULL, 0, &ip_directedbcast, 0,
1506 CTL_NET, PF_INET, IPPROTO_IP,
1507 IPCTL_DIRECTEDBCAST, CTL_EOL);
1508 sysctl_createv(clog, 0, NULL, NULL,
1509 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1510 CTLTYPE_INT, "allowsrcrt",
1511 SYSCTL_DESCR("Accept source-routed datagrams"),
1512 NULL, 0, &ip_allowsrcrt, 0,
1513 CTL_NET, PF_INET, IPPROTO_IP,
1514 IPCTL_ALLOWSRCRT, CTL_EOL);
1515
1516 sysctl_createv(clog, 0, NULL, NULL,
1517 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1518 CTLTYPE_INT, "mtudisc",
1519 SYSCTL_DESCR("Use RFC1191 Path MTU Discovery"),
1520 NULL, 0, &ip_mtudisc, 0,
1521 CTL_NET, PF_INET, IPPROTO_IP,
1522 IPCTL_MTUDISC, CTL_EOL);
1523 sysctl_createv(clog, 0, NULL, NULL,
1524 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1525 CTLTYPE_INT, "anonportmin",
1526 SYSCTL_DESCR("Lowest ephemeral port number to assign"),
1527 sysctl_net_inet_ip_ports, 0, &anonportmin, 0,
1528 CTL_NET, PF_INET, IPPROTO_IP,
1529 IPCTL_ANONPORTMIN, CTL_EOL);
1530 sysctl_createv(clog, 0, NULL, NULL,
1531 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1532 CTLTYPE_INT, "anonportmax",
1533 SYSCTL_DESCR("Highest ephemeral port number to assign"),
1534 sysctl_net_inet_ip_ports, 0, &anonportmax, 0,
1535 CTL_NET, PF_INET, IPPROTO_IP,
1536 IPCTL_ANONPORTMAX, CTL_EOL);
1537 sysctl_createv(clog, 0, NULL, NULL,
1538 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1539 CTLTYPE_INT, "mtudisctimeout",
1540 SYSCTL_DESCR("Lifetime of a Path MTU Discovered route"),
1541 sysctl_net_inet_ip_pmtudto, 0, (void *)&ip_mtudisc_timeout, 0,
1542 CTL_NET, PF_INET, IPPROTO_IP,
1543 IPCTL_MTUDISCTIMEOUT, CTL_EOL);
1544 #if NGIF > 0
1545 sysctl_createv(clog, 0, NULL, NULL,
1546 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1547 CTLTYPE_INT, "gifttl",
1548 SYSCTL_DESCR("Default TTL for a gif tunnel datagram"),
1549 NULL, 0, &ip_gif_ttl, 0,
1550 CTL_NET, PF_INET, IPPROTO_IP,
1551 IPCTL_GIF_TTL, CTL_EOL);
1552 #endif /* NGIF */
1553 #ifndef IPNOPRIVPORTS
1554 sysctl_createv(clog, 0, NULL, NULL,
1555 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1556 CTLTYPE_INT, "lowportmin",
1557 SYSCTL_DESCR("Lowest privileged ephemeral port number "
1558 "to assign"),
1559 sysctl_net_inet_ip_ports, 0, &lowportmin, 0,
1560 CTL_NET, PF_INET, IPPROTO_IP,
1561 IPCTL_LOWPORTMIN, CTL_EOL);
1562 sysctl_createv(clog, 0, NULL, NULL,
1563 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1564 CTLTYPE_INT, "lowportmax",
1565 SYSCTL_DESCR("Highest privileged ephemeral port number "
1566 "to assign"),
1567 sysctl_net_inet_ip_ports, 0, &lowportmax, 0,
1568 CTL_NET, PF_INET, IPPROTO_IP,
1569 IPCTL_LOWPORTMAX, CTL_EOL);
1570 #endif /* IPNOPRIVPORTS */
1571 #if NGRE > 0
1572 sysctl_createv(clog, 0, NULL, NULL,
1573 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1574 CTLTYPE_INT, "grettl",
1575 SYSCTL_DESCR("Default TTL for a gre tunnel datagram"),
1576 NULL, 0, &ip_gre_ttl, 0,
1577 CTL_NET, PF_INET, IPPROTO_IP,
1578 IPCTL_GRE_TTL, CTL_EOL);
1579 #endif /* NGRE */
1580 sysctl_createv(clog, 0, NULL, NULL,
1581 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1582 CTLTYPE_INT, "checkinterface",
1583 SYSCTL_DESCR("Enable receive side of Strong ES model "
1584 "from RFC1122"),
1585 NULL, 0, &ip_checkinterface, 0,
1586 CTL_NET, PF_INET, IPPROTO_IP,
1587 IPCTL_CHECKINTERFACE, CTL_EOL);
1588 sysctl_createv(clog, 0, NULL, NULL,
1589 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1590 CTLTYPE_INT, "random_id",
1591 SYSCTL_DESCR("Assign random ip_id values"),
1592 NULL, 0, &ip_do_randomid, 0,
1593 CTL_NET, PF_INET, IPPROTO_IP,
1594 IPCTL_RANDOMID, CTL_EOL);
1595 sysctl_createv(clog, 0, NULL, NULL,
1596 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1597 CTLTYPE_INT, "do_loopback_cksum",
1598 SYSCTL_DESCR("Perform IP checksum on loopback"),
1599 NULL, 0, &ip_do_loopback_cksum, 0,
1600 CTL_NET, PF_INET, IPPROTO_IP,
1601 IPCTL_LOOPBACKCKSUM, CTL_EOL);
1602 sysctl_createv(clog, 0, NULL, NULL,
1603 CTLFLAG_PERMANENT,
1604 CTLTYPE_STRUCT, "stats",
1605 SYSCTL_DESCR("IP statistics"),
1606 sysctl_net_inet_ip_stats, 0, NULL, 0,
1607 CTL_NET, PF_INET, IPPROTO_IP, IPCTL_STATS,
1608 CTL_EOL);
1609
1610 /* anonportalgo RFC6056 subtree */
1611 const struct sysctlnode *portalgo_node;
1612 sysctl_createv(clog, 0, NULL, &portalgo_node,
1613 CTLFLAG_PERMANENT,
1614 CTLTYPE_NODE, "anonportalgo",
1615 SYSCTL_DESCR("Anonymous Port Algorithm Selection (RFC 6056)"),
1616 NULL, 0, NULL, 0,
1617 CTL_NET, PF_INET, IPPROTO_IP, CTL_CREATE, CTL_EOL);
1618 sysctl_createv(clog, 0, &portalgo_node, NULL,
1619 CTLFLAG_PERMANENT,
1620 CTLTYPE_STRING, "available",
1621 SYSCTL_DESCR("available algorithms"),
1622 sysctl_portalgo_available, 0, NULL, PORTALGO_MAXLEN,
1623 CTL_CREATE, CTL_EOL);
1624 sysctl_createv(clog, 0, &portalgo_node, NULL,
1625 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1626 CTLTYPE_STRING, "selected",
1627 SYSCTL_DESCR("selected algorithm"),
1628 sysctl_portalgo_selected4, 0, NULL, PORTALGO_MAXLEN,
1629 CTL_CREATE, CTL_EOL);
1630 sysctl_createv(clog, 0, &portalgo_node, NULL,
1631 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1632 CTLTYPE_STRUCT, "reserve",
1633 SYSCTL_DESCR("bitmap of reserved ports"),
1634 sysctl_portalgo_reserve4, 0, NULL, 0,
1635 CTL_CREATE, CTL_EOL);
1636 }
1637
1638 void
1639 ip_statinc(u_int stat)
1640 {
1641
1642 KASSERT(stat < IP_NSTATS);
1643 IP_STATINC(stat);
1644 }
1645