/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 2003 Andre Oppermann, Internet Business Solutions AG4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. The name of the author may not be used to endorse or promote15* products derived from this software without specific prior written16* permission.17*18* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031/*32* ip_fastforward gets its speed from processing the forwarded packet to33* completion (if_output on the other side) without any queues or netisr's.34* The receiving interface DMAs the packet into memory, the upper half of35* driver calls ip_fastforward, we do our routing table lookup and directly36* send it off to the outgoing interface, which DMAs the packet to the37* network card. The only part of the packet we touch with the CPU is the38* IP header (unless there are complex firewall rules touching other parts39* of the packet, but that is up to you). We are essentially limited by bus40* bandwidth and how fast the network card/driver can set up receives and41* transmits.42*43* We handle basic errors, IP header errors, checksum errors,44* destination unreachable, fragmentation and fragmentation needed and45* report them via ICMP to the sender.46*47* Else if something is not pure IPv4 unicast forwarding we fall back to48* the normal ip_input processing path. We should only be called from49* interfaces connected to the outside world.50*51* Firewalling is fully supported including divert, ipfw fwd and ipfilter52* ipnat and address rewrite.53*54* IPSEC is not supported if this host is a tunnel broker. IPSEC is55* supported for connections to/from local host.56*57* We try to do the least expensive (in CPU ops) checks and operations58* first to catch junk with as little overhead as possible.59*60* We take full advantage of hardware support for IP checksum and61* fragmentation offloading.62*/6364/*65* Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which66* is being followed here.67*/6869#include "opt_ipstealth.h"70#include "opt_sctp.h"7172#include <sys/param.h>73#include <sys/systm.h>74#include <sys/kernel.h>75#include <sys/malloc.h>76#include <sys/mbuf.h>77#include <sys/protosw.h>78#include <sys/sdt.h>79#include <sys/socket.h>80#include <sys/sysctl.h>8182#include <net/if.h>83#include <net/if_types.h>84#include <net/if_var.h>85#include <net/if_dl.h>86#include <net/if_private.h>87#include <net/pfil.h>88#include <net/route.h>89#include <net/route/nhop.h>90#include <net/vnet.h>9192#include <netinet/in.h>93#include <netinet/in_fib.h>94#include <netinet/in_kdtrace.h>95#include <netinet/in_systm.h>96#include <netinet/in_var.h>97#include <netinet/ip.h>98#include <netinet/ip_var.h>99#include <netinet/ip_icmp.h>100#include <netinet/ip_options.h>101102#include <machine/in_cksum.h>103104#if defined(SCTP) || defined(SCTP_SUPPORT)105#include <netinet/sctp_crc32.h>106#endif107108#define V_ipsendredirects VNET(ipsendredirects)109110static struct mbuf *111ip_redir_alloc(struct mbuf *m, struct nhop_object *nh, u_short ip_len,112struct in_addr *osrc, struct in_addr *newgw)113{114struct in_ifaddr *nh_ia;115struct mbuf *mcopy;116117KASSERT(nh != NULL, ("%s: m %p nh is NULL\n", __func__, m));118119/*120* Only send a redirect if:121* - Redirects are not disabled (must be checked by caller),122* - We have not applied NAT (must be checked by caller as possible),123* - Neither a MCAST or BCAST packet (must be checked by caller)124* [RFC1009 Appendix A.2].125* - The packet does not do IP source routing or having any other126* IP options (this case was handled already by ip_input() calling127* ip_dooptions() [RFC792, p13],128* - The packet is being forwarded out the same physical interface129* that it was received from [RFC1812, 5.2.7.2].130*/131132/*133* - The forwarding route was not created by a redirect134* [RFC1812, 5.2.7.2], or135* if it was to follow a default route (see below).136* - The next-hop is reachable by us [RFC1009 Appendix A.2].137*/138if ((nh->nh_flags & (NHF_DEFAULT | NHF_REDIRECT |139NHF_BLACKHOLE | NHF_REJECT)) != 0)140return (NULL);141142/* Get the new gateway. */143if ((nh->nh_flags & NHF_GATEWAY) == 0 || nh->gw_sa.sa_family != AF_INET)144return (NULL);145newgw->s_addr = nh->gw4_sa.sin_addr.s_addr;146147/*148* - The resulting forwarding destination is not "This host on this149* network" [RFC1122, Section 3.2.1.3] (default route check above).150*/151if (newgw->s_addr == 0)152return (NULL);153154/*155* - We know how to reach the sender and the source address is156* directly connected to us [RFC792, p13].157* + The new gateway address and the source address are on the same158* subnet [RFC1009 Appendix A.2, RFC1122 3.2.2.2, RFC1812, 5.2.7.2].159* NB: if you think multiple logical subnets on the same wire should160* receive redirects read [RFC1812, APPENDIX C (14->15)].161*/162nh_ia = (struct in_ifaddr *)nh->nh_ifa;163if ((ntohl(osrc->s_addr) & nh_ia->ia_subnetmask) != nh_ia->ia_subnet)164return (NULL);165166/* Prepare for sending the redirect. */167168/*169* Make a copy of as much as we need of the packet as the original170* one will be forwarded but we need (a portion) for icmp_error().171*/172mcopy = m_gethdr(M_NOWAIT, m->m_type);173if (mcopy == NULL)174return (NULL);175176if (m_dup_pkthdr(mcopy, m, M_NOWAIT) == 0) {177/*178* It's probably ok if the pkthdr dup fails (because179* the deep copy of the tag chain failed), but for now180* be conservative and just discard the copy since181* code below may some day want the tags.182*/183m_free(mcopy);184return (NULL);185}186mcopy->m_len = min(ip_len, M_TRAILINGSPACE(mcopy));187mcopy->m_pkthdr.len = mcopy->m_len;188m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));189190return (mcopy);191}192193194static int195ip_findroute(struct nhop_object **pnh, struct in_addr dest, struct mbuf *m)196{197struct nhop_object *nh;198199nh = fib4_lookup(M_GETFIB(m), dest, 0, NHR_NONE,200m->m_pkthdr.flowid);201if (nh == NULL) {202IPSTAT_INC(ips_noroute);203IPSTAT_INC(ips_cantforward);204icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);205return (EHOSTUNREACH);206}207/*208* Drop blackholed traffic and directed broadcasts.209*/210if ((nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) != 0) {211IPSTAT_INC(ips_cantforward);212m_freem(m);213return (EHOSTUNREACH);214}215216if (nh->nh_flags & NHF_REJECT) {217IPSTAT_INC(ips_cantforward);218icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);219return (EHOSTUNREACH);220}221222*pnh = nh;223224return (0);225}226227/*228* Try to forward a packet based on the destination address.229* This is a fast path optimized for the plain forwarding case.230* If the packet is handled (and consumed) here then we return NULL;231* otherwise mbuf is returned and the packet should be delivered232* to ip_input for full processing.233*/234struct mbuf *235ip_tryforward(struct mbuf *m)236{237struct ip *ip;238struct mbuf *m0 = NULL;239struct nhop_object *nh = NULL;240struct route ro;241struct sockaddr_in *dst;242const struct sockaddr *gw;243struct in_addr dest, odest, rtdest, osrc;244uint16_t ip_len, ip_off;245int error = 0;246struct m_tag *fwd_tag = NULL;247struct mbuf *mcopy = NULL;248struct in_addr redest;249/*250* Are we active and forwarding packets?251*/252253M_ASSERTVALID(m);254M_ASSERTPKTHDR(m);255256/*257* Only IP packets without options258*/259ip = mtod(m, struct ip *);260261if (ip->ip_hl != (sizeof(struct ip) >> 2)) {262if (V_ip_doopts == 1)263return m;264else if (V_ip_doopts == 2) {265icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,2660, 0);267return NULL; /* mbuf already free'd */268}269/* else ignore IP options and continue */270}271272/*273* Only unicast IP, not from loopback, no L2 or IP broadcast,274* no multicast, no INADDR_ANY275*276* XXX: Probably some of these checks could be direct drop277* conditions. However it is not clear whether there are some278* hacks or obscure behaviours which make it necessary to279* let ip_input handle it. We play safe here and let ip_input280* deal with it until it is proven that we can directly drop it.281*/282if ((m->m_flags & (M_BCAST|M_MCAST)) ||283(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||284in_broadcast(ip->ip_src) ||285in_broadcast(ip->ip_dst) ||286IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||287IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||288IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) ||289IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) )290return m;291292/*293* Is it for a local address on this host?294*/295if (in_localip(ip->ip_dst))296return m;297298IPSTAT_INC(ips_total);299300/*301* Step 3: incoming packet firewall processing302*/303304odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;305osrc.s_addr = ip->ip_src.s_addr;306307/*308* Run through list of ipfilter hooks for input packets309*/310if (!PFIL_HOOKED_IN(V_inet_pfil_head))311goto passin;312313if (pfil_mbuf_in(V_inet_pfil_head, &m, m->m_pkthdr.rcvif,314NULL) != PFIL_PASS)315goto drop;316317M_ASSERTVALID(m);318M_ASSERTPKTHDR(m);319320ip = mtod(m, struct ip *); /* m may have changed by pfil hook */321dest.s_addr = ip->ip_dst.s_addr;322323/*324* Destination address changed?325*/326if (odest.s_addr != dest.s_addr) {327/*328* Is it now for a local address on this host?329*/330if (in_localip(dest))331goto forwardlocal;332/*333* Go on with new destination address334*/335}336337if (m->m_flags & M_FASTFWD_OURS) {338/*339* ipfw changed it for a local address on this host.340*/341goto forwardlocal;342}343344passin:345/*346* Step 4: decrement TTL and look up route347*/348349/*350* Check TTL351*/352#ifdef IPSTEALTH353if (!V_ipstealth) {354#endif355if (ip->ip_ttl <= IPTTLDEC) {356icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);357return NULL; /* mbuf already free'd */358}359360/*361* Decrement the TTL.362* If the IP header checksum field contains a valid value, incrementally363* change this value. Don't use hw checksum offloading, which would364* recompute the checksum. It's faster to just change it here365* according to the decremented TTL.366* If the checksum still needs to be computed, don't touch it.367*/368ip->ip_ttl -= IPTTLDEC;369if (__predict_true((m->m_pkthdr.csum_flags & CSUM_IP) == 0)) {370if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))371ip->ip_sum -= ~htons(IPTTLDEC << 8);372else373ip->ip_sum += htons(IPTTLDEC << 8);374}375#ifdef IPSTEALTH376}377#endif378379/*380* Next hop forced by pfil(9) hook?381*/382if ((m->m_flags & M_IP_NEXTHOP) &&383((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {384/*385* Now we will find route to forced destination.386*/387dest.s_addr = ((struct sockaddr_in *)388(fwd_tag + 1))->sin_addr.s_addr;389m_tag_delete(m, fwd_tag);390m->m_flags &= ~M_IP_NEXTHOP;391}392393/*394* Find route to destination.395*/396if (ip_findroute(&nh, dest, m) != 0)397return (NULL); /* icmp unreach already sent */398399/*400* Avoid second route lookup by caching destination.401*/402rtdest.s_addr = dest.s_addr;403404/*405* Step 5: outgoing firewall packet processing406*/407if (!PFIL_HOOKED_OUT(V_inet_pfil_head))408goto passout;409410if (pfil_mbuf_fwd(V_inet_pfil_head, &m, nh->nh_ifp,411NULL) != PFIL_PASS)412goto drop;413414M_ASSERTVALID(m);415M_ASSERTPKTHDR(m);416417ip = mtod(m, struct ip *);418dest.s_addr = ip->ip_dst.s_addr;419420/*421* Destination address changed?422*/423if (m->m_flags & M_IP_NEXTHOP)424fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);425else426fwd_tag = NULL;427if (odest.s_addr != dest.s_addr || fwd_tag != NULL) {428/*429* Is it now for a local address on this host?430*/431if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) {432forwardlocal:433/*434* Return packet for processing by ip_input().435*/436m->m_flags |= M_FASTFWD_OURS;437return (m);438}439/*440* Redo route lookup with new destination address441*/442if (fwd_tag) {443dest.s_addr = ((struct sockaddr_in *)444(fwd_tag + 1))->sin_addr.s_addr;445m_tag_delete(m, fwd_tag);446m->m_flags &= ~M_IP_NEXTHOP;447}448if (dest.s_addr != rtdest.s_addr &&449ip_findroute(&nh, dest, m) != 0)450return (NULL); /* icmp unreach already sent */451}452453passout:454/*455* Step 6: send off the packet456*/457ip_len = ntohs(ip->ip_len);458ip_off = ntohs(ip->ip_off);459460bzero(&ro, sizeof(ro));461dst = (struct sockaddr_in *)&ro.ro_dst;462dst->sin_family = AF_INET;463dst->sin_len = sizeof(*dst);464dst->sin_addr = dest;465if (nh->nh_flags & NHF_GATEWAY) {466gw = &nh->gw_sa;467ro.ro_flags |= RT_HAS_GW;468} else469gw = (const struct sockaddr *)dst;470471/*472* If the IP/SCTP/TCP/UDP header still needs a valid checksum and the473* interface will not calculate it for us, do it here.474* Note that if we defer checksum calculation, we might send an ICMP475* message later that reflects this packet, which still has an476* invalid checksum.477*/478if (__predict_false(m->m_pkthdr.csum_flags & CSUM_IP &479~nh->nh_ifp->if_hwassist)) {480ip->ip_sum = 0;481ip->ip_sum = in_cksum(m, (ip->ip_hl << 2));482m->m_pkthdr.csum_flags &= ~CSUM_IP;483}484if (__predict_false(m->m_pkthdr.csum_flags & CSUM_DELAY_DATA &485~nh->nh_ifp->if_hwassist)) {486in_delayed_cksum(m);487m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;488}489#if defined(SCTP) || defined(SCTP_SUPPORT)490if (__predict_false(m->m_pkthdr.csum_flags & CSUM_IP_SCTP &491~nh->nh_ifp->if_hwassist)) {492sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));493m->m_pkthdr.csum_flags &= ~CSUM_IP_SCTP;494}495#endif496497/* Handle redirect case. */498redest.s_addr = 0;499if (V_ipsendredirects && osrc.s_addr == ip->ip_src.s_addr &&500nh->nh_ifp == m->m_pkthdr.rcvif)501mcopy = ip_redir_alloc(m, nh, ip_len, &osrc, &redest);502503/*504* Check if packet fits MTU or if hardware will fragment for us505*/506if (ip_len <= nh->nh_mtu) {507/*508* Avoid confusing lower layers.509*/510m_clrprotoflags(m);511/*512* Send off the packet via outgoing interface513*/514IP_PROBE(send, NULL, NULL, ip, nh->nh_ifp, ip, NULL);515error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m, gw, &ro);516} else {517/*518* Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery519*/520if (ip_off & IP_DF) {521IPSTAT_INC(ips_cantfrag);522icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,5230, nh->nh_mtu);524goto consumed;525} else {526/*527* We have to fragment the packet528*/529m->m_pkthdr.csum_flags |= CSUM_IP;530if (ip_fragment(ip, &m, nh->nh_mtu,531nh->nh_ifp->if_hwassist) != 0)532goto drop;533KASSERT(m != NULL, ("null mbuf and no error"));534/*535* Send off the fragments via outgoing interface536*/537error = 0;538do {539m0 = m->m_nextpkt;540m->m_nextpkt = NULL;541/*542* Avoid confusing lower layers.543*/544m_clrprotoflags(m);545546IP_PROBE(send, NULL, NULL,547mtod(m, struct ip *), nh->nh_ifp,548mtod(m, struct ip *), NULL);549error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m,550gw, &ro);551if (error)552break;553} while ((m = m0) != NULL);554if (error) {555/* Reclaim remaining fragments */556for (m = m0; m; m = m0) {557m0 = m->m_nextpkt;558m_freem(m);559}560} else561IPSTAT_INC(ips_fragmented);562}563}564565if (error != 0)566IPSTAT_INC(ips_odropped);567else {568IPSTAT_INC(ips_forward);569IPSTAT_INC(ips_fastforward);570}571572/* Send required redirect */573if (mcopy != NULL) {574icmp_error(mcopy, ICMP_REDIRECT, ICMP_REDIRECT_HOST, redest.s_addr, 0);575mcopy = NULL; /* Was consumed by callee. */576}577578consumed:579if (mcopy != NULL)580m_freem(mcopy);581return NULL;582drop:583if (m)584m_freem(m);585return NULL;586}587588589