2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2003 Andre Oppermann, Internet Business Solutions AG
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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
33 * ip_fastforward gets its speed from processing the forwarded packet to
34 * completion (if_output on the other side) without any queues or netisr's.
35 * The receiving interface DMAs the packet into memory, the upper half of
36 * driver calls ip_fastforward, we do our routing table lookup and directly
37 * send it off to the outgoing interface, which DMAs the packet to the
38 * network card. The only part of the packet we touch with the CPU is the
39 * IP header (unless there are complex firewall rules touching other parts
40 * of the packet, but that is up to you). We are essentially limited by bus
41 * bandwidth and how fast the network card/driver can set up receives and
44 * We handle basic errors, IP header errors, checksum errors,
45 * destination unreachable, fragmentation and fragmentation needed and
46 * report them via ICMP to the sender.
48 * Else if something is not pure IPv4 unicast forwarding we fall back to
49 * the normal ip_input processing path. We should only be called from
50 * interfaces connected to the outside world.
52 * Firewalling is fully supported including divert, ipfw fwd and ipfilter
53 * ipnat and address rewrite.
55 * IPSEC is not supported if this host is a tunnel broker. IPSEC is
56 * supported for connections to/from local host.
58 * We try to do the least expensive (in CPU ops) checks and operations
59 * first to catch junk with as little overhead as possible.
61 * We take full advantage of hardware support for IP checksum and
62 * fragmentation offloading.
64 * We don't do ICMP redirect in the fast forwarding path. I have had my own
65 * cases where two core routers with Zebra routing suite would send millions
66 * ICMP redirects to connected hosts if the destination router was not the
67 * default gateway. In one case it was filling the routing table of a host
68 * with approximately 300.000 cloned redirect entries until it ran out of
69 * kernel memory. However the networking code proved very robust and it didn't
70 * crash or fail in other ways.
74 * Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which
75 * is being followed here.
78 #include <sys/cdefs.h>
79 __FBSDID("$FreeBSD$");
81 #include "opt_ipstealth.h"
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
86 #include <sys/malloc.h>
88 #include <sys/protosw.h>
90 #include <sys/socket.h>
91 #include <sys/sysctl.h>
94 #include <net/if_types.h>
95 #include <net/if_var.h>
96 #include <net/if_dl.h>
98 #include <net/route.h>
99 #include <net/route/nhop.h>
100 #include <net/vnet.h>
102 #include <netinet/in.h>
103 #include <netinet/in_fib.h>
104 #include <netinet/in_kdtrace.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/in_var.h>
107 #include <netinet/ip.h>
108 #include <netinet/ip_var.h>
109 #include <netinet/ip_icmp.h>
110 #include <netinet/ip_options.h>
112 #include <machine/in_cksum.h>
115 ip_findroute(struct nhop_object **pnh, struct in_addr dest, struct mbuf *m)
117 struct nhop_object *nh;
119 nh = fib4_lookup(M_GETFIB(m), dest, 0, NHR_NONE,
122 IPSTAT_INC(ips_noroute);
123 IPSTAT_INC(ips_cantforward);
124 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
125 return (EHOSTUNREACH);
128 * Drop blackholed traffic and directed broadcasts.
130 if ((nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) != 0) {
131 IPSTAT_INC(ips_cantforward);
133 return (EHOSTUNREACH);
136 if (nh->nh_flags & NHF_REJECT) {
137 IPSTAT_INC(ips_cantforward);
138 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
139 return (EHOSTUNREACH);
148 * Try to forward a packet based on the destination address.
149 * This is a fast path optimized for the plain forwarding case.
150 * If the packet is handled (and consumed) here then we return NULL;
151 * otherwise mbuf is returned and the packet should be delivered
152 * to ip_input for full processing.
155 ip_tryforward(struct mbuf *m)
158 struct mbuf *m0 = NULL;
159 struct nhop_object *nh;
160 struct sockaddr_in dst;
161 struct in_addr dest, odest, rtdest;
162 uint16_t ip_len, ip_off;
164 struct m_tag *fwd_tag = NULL;
167 * Are we active and forwarding packets?
175 * Is packet dropped by traffic conditioner?
177 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
182 * Only IP packets without options
184 ip = mtod(m, struct ip *);
186 if (ip->ip_hl != (sizeof(struct ip) >> 2)) {
187 if (V_ip_doopts == 1)
189 else if (V_ip_doopts == 2) {
190 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,
192 return NULL; /* mbuf already free'd */
194 /* else ignore IP options and continue */
198 * Only unicast IP, not from loopback, no L2 or IP broadcast,
199 * no multicast, no INADDR_ANY
201 * XXX: Probably some of these checks could be direct drop
202 * conditions. However it is not clear whether there are some
203 * hacks or obscure behaviours which make it necessary to
204 * let ip_input handle it. We play safe here and let ip_input
205 * deal with it until it is proven that we can directly drop it.
207 if ((m->m_flags & (M_BCAST|M_MCAST)) ||
208 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
209 ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
210 ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
211 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
212 IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
213 IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) ||
214 IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
215 ip->ip_src.s_addr == INADDR_ANY ||
216 ip->ip_dst.s_addr == INADDR_ANY )
220 * Is it for a local address on this host?
222 if (in_localip(ip->ip_dst))
225 IPSTAT_INC(ips_total);
228 * Step 3: incoming packet firewall processing
231 odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;
234 * Run through list of ipfilter hooks for input packets
236 if (!PFIL_HOOKED_IN(V_inet_pfil_head))
239 if (pfil_run_hooks(V_inet_pfil_head, &m, m->m_pkthdr.rcvif, PFIL_IN,
246 ip = mtod(m, struct ip *); /* m may have changed by pfil hook */
247 dest.s_addr = ip->ip_dst.s_addr;
250 * Destination address changed?
252 if (odest.s_addr != dest.s_addr) {
254 * Is it now for a local address on this host?
256 if (in_localip(dest))
259 * Go on with new destination address
263 if (m->m_flags & M_FASTFWD_OURS) {
265 * ipfw changed it for a local address on this host.
272 * Step 4: decrement TTL and look up route
281 if (ip->ip_ttl <= IPTTLDEC) {
282 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
283 return NULL; /* mbuf already free'd */
287 * Decrement the TTL and incrementally change the IP header checksum.
288 * Don't bother doing this with hw checksum offloading, it's faster
289 * doing it right here.
291 ip->ip_ttl -= IPTTLDEC;
292 if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
293 ip->ip_sum -= ~htons(IPTTLDEC << 8);
295 ip->ip_sum += htons(IPTTLDEC << 8);
301 * Next hop forced by pfil(9) hook?
303 if ((m->m_flags & M_IP_NEXTHOP) &&
304 ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {
306 * Now we will find route to forced destination.
308 dest.s_addr = ((struct sockaddr_in *)
309 (fwd_tag + 1))->sin_addr.s_addr;
310 m_tag_delete(m, fwd_tag);
311 m->m_flags &= ~M_IP_NEXTHOP;
315 * Find route to destination.
317 if (ip_findroute(&nh, dest, m) != 0)
318 return (NULL); /* icmp unreach already sent */
321 * Avoid second route lookup by caching destination.
323 rtdest.s_addr = dest.s_addr;
326 * Step 5: outgoing firewall packet processing
328 if (!PFIL_HOOKED_OUT(V_inet_pfil_head))
331 if (pfil_run_hooks(V_inet_pfil_head, &m, nh->nh_ifp,
332 PFIL_OUT | PFIL_FWD, NULL) != PFIL_PASS)
338 ip = mtod(m, struct ip *);
339 dest.s_addr = ip->ip_dst.s_addr;
342 * Destination address changed?
344 if (m->m_flags & M_IP_NEXTHOP)
345 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
348 if (odest.s_addr != dest.s_addr || fwd_tag != NULL) {
350 * Is it now for a local address on this host?
352 if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) {
355 * Return packet for processing by ip_input().
357 m->m_flags |= M_FASTFWD_OURS;
361 * Redo route lookup with new destination address
364 dest.s_addr = ((struct sockaddr_in *)
365 (fwd_tag + 1))->sin_addr.s_addr;
366 m_tag_delete(m, fwd_tag);
367 m->m_flags &= ~M_IP_NEXTHOP;
369 if (dest.s_addr != rtdest.s_addr &&
370 ip_findroute(&nh, dest, m) != 0)
371 return (NULL); /* icmp unreach already sent */
376 * Step 6: send off the packet
378 ip_len = ntohs(ip->ip_len);
379 ip_off = ntohs(ip->ip_off);
381 bzero(&dst, sizeof(dst));
382 dst.sin_family = AF_INET;
383 dst.sin_len = sizeof(dst);
384 if (nh->nh_flags & NHF_GATEWAY)
385 dst.sin_addr = nh->gw4_sa.sin_addr;
390 * Check if packet fits MTU or if hardware will fragment for us
392 if (ip_len <= nh->nh_mtu) {
394 * Avoid confusing lower layers.
398 * Send off the packet via outgoing interface
400 IP_PROBE(send, NULL, NULL, ip, nh->nh_ifp, ip, NULL);
401 error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m,
402 (struct sockaddr *)&dst, NULL);
405 * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery
407 if (ip_off & IP_DF) {
408 IPSTAT_INC(ips_cantfrag);
409 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
414 * We have to fragment the packet
416 m->m_pkthdr.csum_flags |= CSUM_IP;
417 if (ip_fragment(ip, &m, nh->nh_mtu,
418 nh->nh_ifp->if_hwassist) != 0)
420 KASSERT(m != NULL, ("null mbuf and no error"));
422 * Send off the fragments via outgoing interface
429 * Avoid confusing lower layers.
433 IP_PROBE(send, NULL, NULL,
434 mtod(m, struct ip *), nh->nh_ifp,
435 mtod(m, struct ip *), NULL);
436 error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m,
437 (struct sockaddr *)&dst, NULL);
440 } while ((m = m0) != NULL);
442 /* Reclaim remaining fragments */
443 for (m = m0; m; m = m0) {
448 IPSTAT_INC(ips_fragmented);
453 IPSTAT_INC(ips_odropped);
455 IPSTAT_INC(ips_forward);
456 IPSTAT_INC(ips_fastforward);