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