2 * Copyright (c) 2003 Andre Oppermann, Internet Business Solutions AG
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * ip_fastforward gets its speed from processing the forwarded packet to
32 * completion (if_output on the other side) without any queues or netisr's.
33 * The receiving interface DMAs the packet into memory, the upper half of
34 * driver calls ip_fastforward, we do our routing table lookup and directly
35 * send it off to the outgoing interface, which DMAs the packet to the
36 * network card. The only part of the packet we touch with the CPU is the
37 * IP header (unless there are complex firewall rules touching other parts
38 * of the packet, but that is up to you). We are essentially limited by bus
39 * bandwidth and how fast the network card/driver can set up receives and
42 * We handle basic errors, IP header errors, checksum errors,
43 * destination unreachable, fragmentation and fragmentation needed and
44 * report them via ICMP to the sender.
46 * Else if something is not pure IPv4 unicast forwarding we fall back to
47 * the normal ip_input processing path. We should only be called from
48 * interfaces connected to the outside world.
50 * Firewalling is fully supported including divert, ipfw fwd and ipfilter
51 * ipnat and address rewrite.
53 * IPSEC is not supported if this host is a tunnel broker. IPSEC is
54 * supported for connections to/from local host.
56 * We try to do the least expensive (in CPU ops) checks and operations
57 * first to catch junk with as little overhead as possible.
59 * We take full advantage of hardware support for IP checksum and
60 * fragmentation offloading.
62 * We don't do ICMP redirect in the fast forwarding path. I have had my own
63 * cases where two core routers with Zebra routing suite would send millions
64 * ICMP redirects to connected hosts if the destination router was not the
65 * default gateway. In one case it was filling the routing table of a host
66 * with approximately 300.000 cloned redirect entries until it ran out of
67 * kernel memory. However the networking code proved very robust and it didn't
68 * crash or fail in other ways.
72 * Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which
73 * is being followed here.
76 #include <sys/cdefs.h>
77 __FBSDID("$FreeBSD$");
80 #include "opt_ipstealth.h"
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.h>
85 #include <sys/malloc.h>
87 #include <sys/protosw.h>
88 #include <sys/socket.h>
89 #include <sys/sysctl.h>
93 #include <net/if_types.h>
94 #include <net/if_var.h>
95 #include <net/if_dl.h>
96 #include <net/route.h>
98 #include <netinet/in.h>
99 #include <netinet/in_systm.h>
100 #include <netinet/in_var.h>
101 #include <netinet/ip.h>
102 #include <netinet/ip_var.h>
103 #include <netinet/ip_icmp.h>
104 #include <netinet/ip_options.h>
106 #include <machine/in_cksum.h>
108 static int ipfastforward_active = 0;
109 SYSCTL_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW,
110 &ipfastforward_active, 0, "Enable fast IP forwarding");
112 static struct sockaddr_in *
113 ip_findroute(struct route *ro, struct in_addr dest, struct mbuf *m)
115 struct sockaddr_in *dst;
119 * Find route to destination.
121 bzero(ro, sizeof(*ro));
122 dst = (struct sockaddr_in *)&ro->ro_dst;
123 dst->sin_family = AF_INET;
124 dst->sin_len = sizeof(*dst);
125 dst->sin_addr.s_addr = dest.s_addr;
126 in_rtalloc_ign(ro, RTF_CLONING, M_GETFIB(m));
129 * Route there and interface still up?
132 if (rt && (rt->rt_flags & RTF_UP) &&
133 (rt->rt_ifp->if_flags & IFF_UP) &&
134 (rt->rt_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
135 if (rt->rt_flags & RTF_GATEWAY)
136 dst = (struct sockaddr_in *)rt->rt_gateway;
138 ipstat.ips_noroute++;
139 ipstat.ips_cantforward++;
142 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
149 * Try to forward a packet based on the destination address.
150 * This is a fast path optimized for the plain forwarding case.
151 * If the packet is handled (and consumed) here then we return 1;
152 * otherwise 0 is returned and the packet should be delivered
153 * to ip_input for full processing.
156 ip_fastforward(struct mbuf *m)
159 struct mbuf *m0 = NULL;
161 struct sockaddr_in *dst = NULL;
163 struct in_addr odest, dest;
167 #ifdef IPFIREWALL_FORWARD
168 struct m_tag *fwd_tag;
172 * Are we active and forwarding packets?
174 if (!ipfastforward_active || !ipforwarding)
183 * Step 1: check for packet drop conditions (and sanity checks)
187 * Is entire packet big enough?
189 if (m->m_pkthdr.len < sizeof(struct ip)) {
190 ipstat.ips_tooshort++;
195 * Is first mbuf large enough for ip header and is header present?
197 if (m->m_len < sizeof (struct ip) &&
198 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
199 ipstat.ips_toosmall++;
200 return NULL; /* mbuf already free'd */
203 ip = mtod(m, struct ip *);
208 if (ip->ip_v != IPVERSION) {
209 ipstat.ips_badvers++;
214 * Is IP header length correct and is it in first mbuf?
216 hlen = ip->ip_hl << 2;
217 if (hlen < sizeof(struct ip)) { /* minimum header length */
221 if (hlen > m->m_len) {
222 if ((m = m_pullup(m, hlen)) == NULL) {
223 ipstat.ips_badhlen++;
224 return NULL; /* mbuf already free'd */
226 ip = mtod(m, struct ip *);
232 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED)
233 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
235 if (hlen == sizeof(struct ip))
236 sum = in_cksum_hdr(ip);
238 sum = in_cksum(m, hlen);
246 * Remember that we have checked the IP header and found it valid.
248 m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
250 ip_len = ntohs(ip->ip_len);
253 * Is IP length longer than packet we have got?
255 if (m->m_pkthdr.len < ip_len) {
256 ipstat.ips_tooshort++;
261 * Is packet longer than IP header tells us? If yes, truncate packet.
263 if (m->m_pkthdr.len > ip_len) {
264 if (m->m_len == m->m_pkthdr.len) {
266 m->m_pkthdr.len = ip_len;
268 m_adj(m, ip_len - m->m_pkthdr.len);
272 * Is packet from or to 127/8?
274 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
275 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
276 ipstat.ips_badaddr++;
282 * Is packet dropped by traffic conditioner?
284 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
289 * Step 2: fallback conditions to normal ip_input path processing
293 * Only IP packets without options
295 if (ip->ip_hl != (sizeof(struct ip) >> 2)) {
298 else if (ip_doopts == 2) {
299 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,
301 return NULL; /* mbuf already free'd */
303 /* else ignore IP options and continue */
307 * Only unicast IP, not from loopback, no L2 or IP broadcast,
308 * no multicast, no INADDR_ANY
310 * XXX: Probably some of these checks could be direct drop
311 * conditions. However it is not clear whether there are some
312 * hacks or obscure behaviours which make it neccessary to
313 * let ip_input handle it. We play safe here and let ip_input
314 * deal with it until it is proven that we can directly drop it.
316 if ((m->m_flags & (M_BCAST|M_MCAST)) ||
317 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
318 ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
319 ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
320 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
321 IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
322 IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) ||
323 IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
324 ip->ip_src.s_addr == INADDR_ANY ||
325 ip->ip_dst.s_addr == INADDR_ANY )
329 * Is it for a local address on this host?
331 if (in_localip(ip->ip_dst))
337 * Step 3: incoming packet firewall processing
341 * Convert to host representation
343 ip->ip_len = ntohs(ip->ip_len);
344 ip->ip_off = ntohs(ip->ip_off);
346 odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;
349 * Run through list of ipfilter hooks for input packets
351 if (!PFIL_HOOKED(&inet_pfil_hook))
354 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL) ||
361 ip = mtod(m, struct ip *); /* m may have changed by pfil hook */
362 dest.s_addr = ip->ip_dst.s_addr;
365 * Destination address changed?
367 if (odest.s_addr != dest.s_addr) {
369 * Is it now for a local address on this host?
371 if (in_localip(dest))
374 * Go on with new destination address
377 #ifdef IPFIREWALL_FORWARD
378 if (m->m_flags & M_FASTFWD_OURS) {
380 * ipfw changed it for a local address on this host.
384 #endif /* IPFIREWALL_FORWARD */
388 * Step 4: decrement TTL and look up route
397 if (ip->ip_ttl <= IPTTLDEC) {
398 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
399 return NULL; /* mbuf already free'd */
403 * Decrement the TTL and incrementally change the IP header checksum.
404 * Don't bother doing this with hw checksum offloading, it's faster
405 * doing it right here.
407 ip->ip_ttl -= IPTTLDEC;
408 if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
409 ip->ip_sum -= ~htons(IPTTLDEC << 8);
411 ip->ip_sum += htons(IPTTLDEC << 8);
417 * Find route to destination.
419 if ((dst = ip_findroute(&ro, dest, m)) == NULL)
420 return NULL; /* icmp unreach already sent */
421 ifp = ro.ro_rt->rt_ifp;
424 * Immediately drop blackholed traffic, and directed broadcasts
425 * for either the all-ones or all-zero subnet addresses on
426 * locally attached networks.
428 if ((ro.ro_rt->rt_flags & (RTF_BLACKHOLE|RTF_BROADCAST)) != 0)
432 * Step 5: outgoing firewall packet processing
436 * Run through list of hooks for output packets.
438 if (!PFIL_HOOKED(&inet_pfil_hook))
441 if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, NULL) || m == NULL) {
448 ip = mtod(m, struct ip *);
449 dest.s_addr = ip->ip_dst.s_addr;
452 * Destination address changed?
454 #ifndef IPFIREWALL_FORWARD
455 if (odest.s_addr != dest.s_addr) {
457 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
458 if (odest.s_addr != dest.s_addr || fwd_tag != NULL) {
459 #endif /* IPFIREWALL_FORWARD */
461 * Is it now for a local address on this host?
463 #ifndef IPFIREWALL_FORWARD
464 if (in_localip(dest)) {
466 if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) {
467 #endif /* IPFIREWALL_FORWARD */
470 * Return packet for processing by ip_input().
471 * Keep host byte order as expected at ip_input's
474 m->m_flags |= M_FASTFWD_OURS;
480 * Redo route lookup with new destination address
482 #ifdef IPFIREWALL_FORWARD
484 dest.s_addr = ((struct sockaddr_in *)
485 (fwd_tag + 1))->sin_addr.s_addr;
486 m_tag_delete(m, fwd_tag);
488 #endif /* IPFIREWALL_FORWARD */
490 if ((dst = ip_findroute(&ro, dest, m)) == NULL)
491 return NULL; /* icmp unreach already sent */
492 ifp = ro.ro_rt->rt_ifp;
497 * Step 6: send off the packet
501 * Check if route is dampned (when ARP is unable to resolve)
503 if ((ro.ro_rt->rt_flags & RTF_REJECT) &&
504 (ro.ro_rt->rt_rmx.rmx_expire == 0 ||
505 time_uptime < ro.ro_rt->rt_rmx.rmx_expire)) {
506 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
512 * Check if there is enough space in the interface queue
514 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
515 ifp->if_snd.ifq_maxlen) {
516 ipstat.ips_odropped++;
517 /* would send source quench here but that is depreciated */
523 * Check if media link state of interface is not down
525 if (ifp->if_link_state == LINK_STATE_DOWN) {
526 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
531 * Check if packet fits MTU or if hardware will fragment for us
533 if (ro.ro_rt->rt_rmx.rmx_mtu)
534 mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu);
538 if (ip->ip_len <= mtu ||
539 (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) {
541 * Restore packet header fields to original values
543 ip->ip_len = htons(ip->ip_len);
544 ip->ip_off = htons(ip->ip_off);
546 * Send off the packet via outgoing interface
548 error = (*ifp->if_output)(ifp, m,
549 (struct sockaddr *)dst, ro.ro_rt);
552 * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery
554 if (ip->ip_off & IP_DF) {
555 ipstat.ips_cantfrag++;
556 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
561 * We have to fragment the packet
563 m->m_pkthdr.csum_flags |= CSUM_IP;
565 * ip_fragment expects ip_len and ip_off in host byte
566 * order but returns all packets in network byte order
568 if (ip_fragment(ip, &m, mtu, ifp->if_hwassist,
569 (~ifp->if_hwassist & CSUM_DELAY_IP))) {
572 KASSERT(m != NULL, ("null mbuf and no error"));
574 * Send off the fragments via outgoing interface
581 error = (*ifp->if_output)(ifp, m,
582 (struct sockaddr *)dst, ro.ro_rt);
585 } while ((m = m0) != NULL);
587 /* Reclaim remaining fragments */
588 for (m = m0; m; m = m0) {
593 ipstat.ips_fragmented++;
598 ipstat.ips_odropped++;
600 ro.ro_rt->rt_rmx.rmx_pksent++;
601 ipstat.ips_forward++;
602 ipstat.ips_fastforward++;