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