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
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 router to dest was not the default
67 * gateway. In one case it was filling the routing table of a host with close
68 * 300'000 cloned redirect entries until it ran out of kernel memory. However
69 * the networking code proved very robust and it didn't crash or went ill
74 * Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which
75 * is being followed here.
80 #include "opt_ipdivert.h"
81 #include "opt_ipfilter.h"
82 #include "opt_ipstealth.h"
83 #include "opt_pfil_hooks.h"
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/kernel.h>
88 #include <sys/malloc.h>
90 #include <sys/protosw.h>
91 #include <sys/socket.h>
92 #include <sys/sysctl.h>
96 #include <net/if_types.h>
97 #include <net/if_var.h>
98 #include <net/if_dl.h>
99 #include <net/route.h>
101 #include <netinet/in.h>
102 #include <netinet/in_systm.h>
103 #include <netinet/in_var.h>
104 #include <netinet/ip.h>
105 #include <netinet/ip_var.h>
106 #include <netinet/ip_icmp.h>
108 #include <machine/in_cksum.h>
110 #include <netinet/ip_fw.h>
111 #include <netinet/ip_divert.h>
112 #include <netinet/ip_dummynet.h>
114 static int ipfastforward_active = 0;
115 SYSCTL_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW,
116 &ipfastforward_active, 0, "Enable fast IP forwarding");
118 static struct sockaddr_in *
119 ip_findroute(struct route *ro, struct in_addr dest, struct mbuf *m)
121 struct sockaddr_in *dst;
125 * Find route to destination.
127 bzero(ro, sizeof(*ro));
128 dst = (struct sockaddr_in *)&ro->ro_dst;
129 dst->sin_family = AF_INET;
130 dst->sin_len = sizeof(*dst);
131 dst->sin_addr.s_addr = dest.s_addr;
132 rtalloc_ign(ro, RTF_CLONING);
135 * Route there and interface still up?
138 if (rt && (rt->rt_flags & RTF_UP) &&
139 (rt->rt_ifp->if_flags & IFF_UP) &&
140 (rt->rt_ifp->if_flags & IFF_RUNNING)) {
141 if (rt->rt_flags & RTF_GATEWAY)
142 dst = (struct sockaddr_in *)rt->rt_gateway;
144 ipstat.ips_noroute++;
145 ipstat.ips_cantforward++;
148 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL);
155 * Try to forward a packet based on the destination address.
156 * This is a fast path optimized for the plain forwarding case.
157 * If the packet is handled (and consumed) here then we return 1;
158 * otherwise 0 is returned and the packet should be delivered
159 * to ip_input for full processing.
162 ip_fastforward(struct mbuf *m)
165 struct mbuf *m0 = NULL;
168 struct mbuf *clone = NULL;
171 struct sockaddr_in *dst = NULL;
172 struct in_ifaddr *ia = NULL;
173 struct ifaddr *ifa = NULL;
175 struct ip_fw_args args;
176 struct in_addr odest, dest;
182 * Are we active and forwarding packets?
184 if (!ipfastforward_active || !ipforwarding)
193 * Step 1: check for packet drop conditions (and sanity checks)
197 * Is entire packet big enough?
199 if (m->m_pkthdr.len < sizeof(struct ip)) {
200 ipstat.ips_tooshort++;
205 * Is first mbuf large enough for ip header and is header present?
207 if (m->m_len < sizeof (struct ip) &&
208 (m = m_pullup(m, sizeof (struct ip))) == 0) {
209 ipstat.ips_toosmall++;
213 ip = mtod(m, struct ip *);
218 if (ip->ip_v != IPVERSION) {
219 ipstat.ips_badvers++;
224 * Is IP header length correct and is it in first mbuf?
226 hlen = ip->ip_hl << 2;
227 if (hlen < sizeof(struct ip)) { /* minimum header length */
231 if (hlen > m->m_len) {
232 if ((m = m_pullup(m, hlen)) == 0) {
233 ipstat.ips_badhlen++;
236 ip = mtod(m, struct ip *);
242 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED)
243 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
245 if (hlen == sizeof(struct ip))
246 sum = in_cksum_hdr(ip);
248 sum = in_cksum(m, hlen);
254 m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
256 ip_len = ntohs(ip->ip_len);
259 * Is IP length longer than packet we have got?
261 if (m->m_pkthdr.len < ip_len) {
262 ipstat.ips_tooshort++;
267 * Is packet longer than IP header tells us? If yes, truncate packet.
269 if (m->m_pkthdr.len > ip_len) {
270 if (m->m_len == m->m_pkthdr.len) {
272 m->m_pkthdr.len = ip_len;
274 m_adj(m, ip_len - m->m_pkthdr.len);
278 * Is packet from or to 127/8?
280 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
281 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
282 ipstat.ips_badaddr++;
288 * Is packet dropped by traffic conditioner?
290 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
295 * Step 2: fallback conditions to normal ip_input path processing
299 * Only IP packets without options
301 if (ip->ip_hl != (sizeof(struct ip) >> 2)) {
304 else if (ip_doopts == 2) {
305 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,
309 /* else ignore IP options and continue */
313 * Only unicast IP, not from loopback, no L2 or IP broadcast,
314 * no multicast, no INADDR_ANY
316 * XXX: Probably some of these checks could be direct drop
317 * conditions. However it is not clear whether there are some
318 * hacks or obscure behaviours which make it neccessary to
319 * let ip_input handle it. We play safe here and let ip_input
320 * deal with it until it is proven that we can directly drop it.
322 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
323 ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
324 ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
325 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
326 IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
327 ip->ip_dst.s_addr == INADDR_ANY )
331 * Is it for a local address on this host?
333 if (in_localip(ip->ip_dst))
337 * Or is it for a local IP broadcast address on this host?
339 if ((m->m_flags & M_BCAST) &&
340 (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST)) {
341 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
342 if (ifa->ifa_addr->sa_family != AF_INET)
345 if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
347 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
355 * Step 3: incoming packet firewall processing
359 * Convert to host representation
361 ip->ip_len = ntohs(ip->ip_len);
362 ip->ip_off = ntohs(ip->ip_off);
364 odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;
367 * Run through list of ipfilter hooks for input packets
369 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN) ||
376 ip = mtod(m, struct ip *); /* m may have changed by pfil hook */
377 dest.s_addr = ip->ip_dst.s_addr;
381 * Run through ipfw for input packets
383 if (fw_enable && IPFW_LOADED) {
384 bzero(&args, sizeof(args));
387 ipfw = ip_fw_chk_ptr(&args);
394 * Packet denied, drop it
396 if ((ipfw & IP_FW_PORT_DENY_FLAG) || m == NULL)
399 * Send packet to the appropriate pipe
401 if (DUMMYNET_LOADED && (ipfw & IP_FW_PORT_DYNT_FLAG) != 0) {
402 ip_dn_io_ptr(m, ipfw & 0xffff, DN_TO_IP_IN, &args);
409 if (ipfw != 0 && (ipfw & IP_FW_PORT_DYNT_FLAG) == 0) {
411 * See if this is a fragment
413 if (ip->ip_off & (IP_MF | IP_OFFMASK))
418 if ((ipfw & IP_FW_PORT_TEE_FLAG) != 0)
419 clone = divert_clone(m);
426 * Delayed checksums are not compatible
428 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
430 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
433 * Restore packet header fields to original values
435 tip = mtod(m, struct ip *);
436 tip->ip_len = htons(tip->ip_len);
437 tip->ip_off = htons(tip->ip_off);
439 * Deliver packet to divert input routine
443 * If this was not tee, we are done
446 if ((ipfw & IP_FW_PORT_TEE_FLAG) == 0)
448 /* Continue if it was tee */
452 if (ipfw == 0 && args.next_hop != NULL) {
453 dest.s_addr = args.next_hop->sin_addr.s_addr;
457 * Let through or not?
463 ip = mtod(m, struct ip *); /* if m changed during fw processing */
466 * Destination address changed?
468 if (odest.s_addr != dest.s_addr) {
470 * Is it now for a local address on this host?
472 if (in_localip(dest))
475 * Go on with new destination address
480 * Step 4: decrement TTL and look up route
489 if (ip->ip_ttl <= IPTTLDEC) {
490 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, NULL);
495 * Decrement the TTL and incrementally change the checksum.
496 * Don't bother doing this with hw checksum offloading.
498 ip->ip_ttl -= IPTTLDEC;
499 if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
500 ip->ip_sum -= ~htons(IPTTLDEC << 8);
502 ip->ip_sum += htons(IPTTLDEC << 8);
508 * Find route to destination.
510 if ((dst = ip_findroute(&ro, dest, m)) == NULL)
511 return 1; /* icmp unreach already sent */
512 ifp = ro.ro_rt->rt_ifp;
515 * Step 5: outgoing firewall packet processing
520 * Run through list of hooks for output packets.
522 if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT) || m == NULL) {
529 ip = mtod(m, struct ip *);
530 dest.s_addr = ip->ip_dst.s_addr;
532 if (fw_enable && IPFW_LOADED && !args.next_hop) {
533 bzero(&args, sizeof(args));
537 ipfw = ip_fw_chk_ptr(&args);
543 if ((ipfw & IP_FW_PORT_DENY_FLAG) || m == NULL)
546 if (DUMMYNET_LOADED && (ipfw & IP_FW_PORT_DYNT_FLAG) != 0) {
548 * XXX note: if the ifp or rt entry are deleted
549 * while a pkt is in dummynet, we are in trouble!
551 args.ro = &ro; /* dummynet does not save it */
554 ip_dn_io_ptr(m, ipfw & 0xffff, DN_TO_IP_OUT, &args);
558 if (ipfw != 0 && (ipfw & IP_FW_PORT_DYNT_FLAG) == 0) {
560 * See if this is a fragment
562 if (ip->ip_off & (IP_MF | IP_OFFMASK))
567 if ((ipfw & IP_FW_PORT_TEE_FLAG) != 0)
568 clone = divert_clone(m);
575 * Delayed checksums are not compatible with divert
577 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
579 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
582 * Restore packet header fields to original values
584 tip = mtod(m, struct ip *);
585 tip->ip_len = htons(tip->ip_len);
586 tip->ip_off = htons(tip->ip_off);
588 * Deliver packet to divert input routine
592 * If this was not tee, we are done
595 if ((ipfw & IP_FW_PORT_TEE_FLAG) == 0) {
598 /* Continue if it was tee */
602 if (ipfw == 0 && args.next_hop != NULL) {
603 dest.s_addr = args.next_hop->sin_addr.s_addr;
607 * Let through or not?
613 ip = mtod(m, struct ip *);
616 * Destination address changed?
618 if (odest.s_addr != dest.s_addr) {
620 * Is it now for a local address on this host?
622 if (in_localip(dest)) {
625 struct m_tag *mtag = m_tag_get(
626 PACKET_TAG_IPFORWARD,
627 sizeof(struct sockaddr_in *),
632 *(struct sockaddr_in **)(mtag+1) =
634 m_tag_prepend(m, mtag);
637 droptoours: /* Used for DIVERT */
640 m->m_flags |= M_FASTFWD_OURS;
642 /* ip still points to the real packet */
643 ip->ip_len = htons(ip->ip_len);
644 ip->ip_off = htons(ip->ip_off);
647 * Return packet for processing by ip_input
654 * Redo route lookup with new destination address
657 if ((dst = ip_findroute(&ro, dest, m)) == NULL)
658 return 1; /* icmp unreach already sent */
659 ifp = ro.ro_rt->rt_ifp;
663 * Step 6: send off the packet
667 * Check if route is dampned (when ARP is unable to resolve)
669 if ((ro.ro_rt->rt_flags & RTF_REJECT) &&
670 ro.ro_rt->rt_rmx.rmx_expire >= time_second) {
671 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL);
677 * Check if there is enough space in the interface queue
679 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
680 ifp->if_snd.ifq_maxlen) {
681 ipstat.ips_odropped++;
682 /* would send source quench here but that is depreciated */
688 * Check if media link state of interface is not down
690 if (ifp->if_link_state == LINK_STATE_DOWN) {
691 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL);
696 * Check if packet fits MTU or if hardware will fragement for us
698 if (ro.ro_rt->rt_rmx.rmx_mtu)
699 mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu);
703 if (ip->ip_len <= mtu ||
704 (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) {
706 * Restore packet header fields to original values
708 ip->ip_len = htons(ip->ip_len);
709 ip->ip_off = htons(ip->ip_off);
711 * Send off the packet via outgoing interface
713 error = (*ifp->if_output)(ifp, m,
714 (struct sockaddr *)dst, ro.ro_rt);
717 * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery
719 if (ip->ip_off & IP_DF) {
720 ipstat.ips_cantfrag++;
721 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
726 * We have to fragement the packet
728 m->m_pkthdr.csum_flags |= CSUM_IP;
730 * ip_fragment expects ip_len and ip_off in host byte
731 * order but returns all packets in network byte order
733 if (ip_fragment(ip, &m, mtu, ifp->if_hwassist,
734 (~ifp->if_hwassist & CSUM_DELAY_IP))) {
737 KASSERT(m != NULL, ("null mbuf and no error"));
739 * Send off the fragments via outgoing interface
746 error = (*ifp->if_output)(ifp, m,
747 (struct sockaddr *)dst, ro.ro_rt);
750 } while ((m = m0) != NULL);
752 /* Reclaim remaining fragments */
759 ipstat.ips_fragmented++;
764 ipstat.ips_odropped++;
766 ro.ro_rt->rt_rmx.rmx_pksent++;
767 ipstat.ips_forward++;
768 ipstat.ips_fastforward++;