/*-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 <sys/cdefs.h>70#include "opt_ipstealth.h"71#include "opt_sctp.h"7273#include <sys/param.h>74#include <sys/systm.h>75#include <sys/kernel.h>76#include <sys/malloc.h>77#include <sys/mbuf.h>78#include <sys/protosw.h>79#include <sys/sdt.h>80#include <sys/socket.h>81#include <sys/sysctl.h>8283#include <net/if.h>84#include <net/if_types.h>85#include <net/if_var.h>86#include <net/if_dl.h>87#include <net/if_private.h>88#include <net/pfil.h>89#include <net/route.h>90#include <net/route/nhop.h>91#include <net/vnet.h>9293#include <netinet/in.h>94#include <netinet/in_fib.h>95#include <netinet/in_kdtrace.h>96#include <netinet/in_systm.h>97#include <netinet/in_var.h>98#include <netinet/ip.h>99#include <netinet/ip_var.h>100#include <netinet/ip_icmp.h>101#include <netinet/ip_options.h>102103#include <machine/in_cksum.h>104105#if defined(SCTP) || defined(SCTP_SUPPORT)106#include <netinet/sctp_crc32.h>107#endif108109#define V_ipsendredirects VNET(ipsendredirects)110111static struct mbuf *112ip_redir_alloc(struct mbuf *m, struct nhop_object *nh, u_short ip_len,113struct in_addr *osrc, struct in_addr *newgw)114{115struct in_ifaddr *nh_ia;116struct mbuf *mcopy;117118KASSERT(nh != NULL, ("%s: m %p nh is NULL\n", __func__, m));119120/*121* Only send a redirect if:122* - Redirects are not disabled (must be checked by caller),123* - We have not applied NAT (must be checked by caller as possible),124* - Neither a MCAST or BCAST packet (must be checked by caller)125* [RFC1009 Appendix A.2].126* - The packet does not do IP source routing or having any other127* IP options (this case was handled already by ip_input() calling128* ip_dooptions() [RFC792, p13],129* - The packet is being forwarded out the same physical interface130* that it was received from [RFC1812, 5.2.7.2].131*/132133/*134* - The forwarding route was not created by a redirect135* [RFC1812, 5.2.7.2], or136* if it was to follow a default route (see below).137* - The next-hop is reachable by us [RFC1009 Appendix A.2].138*/139if ((nh->nh_flags & (NHF_DEFAULT | NHF_REDIRECT |140NHF_BLACKHOLE | NHF_REJECT)) != 0)141return (NULL);142143/* Get the new gateway. */144if ((nh->nh_flags & NHF_GATEWAY) == 0 || nh->gw_sa.sa_family != AF_INET)145return (NULL);146newgw->s_addr = nh->gw4_sa.sin_addr.s_addr;147148/*149* - The resulting forwarding destination is not "This host on this150* network" [RFC1122, Section 3.2.1.3] (default route check above).151*/152if (newgw->s_addr == 0)153return (NULL);154155/*156* - We know how to reach the sender and the source address is157* directly connected to us [RFC792, p13].158* + The new gateway address and the source address are on the same159* subnet [RFC1009 Appendix A.2, RFC1122 3.2.2.2, RFC1812, 5.2.7.2].160* NB: if you think multiple logical subnets on the same wire should161* receive redirects read [RFC1812, APPENDIX C (14->15)].162*/163nh_ia = (struct in_ifaddr *)nh->nh_ifa;164if ((ntohl(osrc->s_addr) & nh_ia->ia_subnetmask) != nh_ia->ia_subnet)165return (NULL);166167/* Prepare for sending the redirect. */168169/*170* Make a copy of as much as we need of the packet as the original171* one will be forwarded but we need (a portion) for icmp_error().172*/173mcopy = m_gethdr(M_NOWAIT, m->m_type);174if (mcopy == NULL)175return (NULL);176177if (m_dup_pkthdr(mcopy, m, M_NOWAIT) == 0) {178/*179* It's probably ok if the pkthdr dup fails (because180* the deep copy of the tag chain failed), but for now181* be conservative and just discard the copy since182* code below may some day want the tags.183*/184m_free(mcopy);185return (NULL);186}187mcopy->m_len = min(ip_len, M_TRAILINGSPACE(mcopy));188mcopy->m_pkthdr.len = mcopy->m_len;189m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));190191return (mcopy);192}193194195static int196ip_findroute(struct nhop_object **pnh, struct in_addr dest, struct mbuf *m)197{198struct nhop_object *nh;199200nh = fib4_lookup(M_GETFIB(m), dest, 0, NHR_NONE,201m->m_pkthdr.flowid);202if (nh == NULL) {203IPSTAT_INC(ips_noroute);204IPSTAT_INC(ips_cantforward);205icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);206return (EHOSTUNREACH);207}208/*209* Drop blackholed traffic and directed broadcasts.210*/211if ((nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) != 0) {212IPSTAT_INC(ips_cantforward);213m_freem(m);214return (EHOSTUNREACH);215}216217if (nh->nh_flags & NHF_REJECT) {218IPSTAT_INC(ips_cantforward);219icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);220return (EHOSTUNREACH);221}222223*pnh = nh;224225return (0);226}227228/*229* Try to forward a packet based on the destination address.230* This is a fast path optimized for the plain forwarding case.231* If the packet is handled (and consumed) here then we return NULL;232* otherwise mbuf is returned and the packet should be delivered233* to ip_input for full processing.234*/235struct mbuf *236ip_tryforward(struct mbuf *m)237{238struct ip *ip;239struct mbuf *m0 = NULL;240struct nhop_object *nh = NULL;241struct route ro;242struct sockaddr_in *dst;243const struct sockaddr *gw;244struct in_addr dest, odest, rtdest, osrc;245uint16_t ip_len, ip_off;246int error = 0;247struct m_tag *fwd_tag = NULL;248struct mbuf *mcopy = NULL;249struct in_addr redest;250/*251* Are we active and forwarding packets?252*/253254M_ASSERTVALID(m);255M_ASSERTPKTHDR(m);256257/*258* Only IP packets without options259*/260ip = mtod(m, struct ip *);261262if (ip->ip_hl != (sizeof(struct ip) >> 2)) {263if (V_ip_doopts == 1)264return m;265else if (V_ip_doopts == 2) {266icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,2670, 0);268return NULL; /* mbuf already free'd */269}270/* else ignore IP options and continue */271}272273/*274* Only unicast IP, not from loopback, no L2 or IP broadcast,275* no multicast, no INADDR_ANY276*277* XXX: Probably some of these checks could be direct drop278* conditions. However it is not clear whether there are some279* hacks or obscure behaviours which make it necessary to280* let ip_input handle it. We play safe here and let ip_input281* deal with it until it is proven that we can directly drop it.282*/283if ((m->m_flags & (M_BCAST|M_MCAST)) ||284(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||285in_broadcast(ip->ip_src) ||286in_broadcast(ip->ip_dst) ||287IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||288IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||289IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) ||290IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) )291return m;292293/*294* Is it for a local address on this host?295*/296if (in_localip(ip->ip_dst))297return m;298299IPSTAT_INC(ips_total);300301/*302* Step 3: incoming packet firewall processing303*/304305odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;306osrc.s_addr = ip->ip_src.s_addr;307308/*309* Run through list of ipfilter hooks for input packets310*/311if (!PFIL_HOOKED_IN(V_inet_pfil_head))312goto passin;313314if (pfil_mbuf_in(V_inet_pfil_head, &m, m->m_pkthdr.rcvif,315NULL) != PFIL_PASS)316goto drop;317318M_ASSERTVALID(m);319M_ASSERTPKTHDR(m);320321ip = mtod(m, struct ip *); /* m may have changed by pfil hook */322dest.s_addr = ip->ip_dst.s_addr;323324/*325* Destination address changed?326*/327if (odest.s_addr != dest.s_addr) {328/*329* Is it now for a local address on this host?330*/331if (in_localip(dest))332goto forwardlocal;333/*334* Go on with new destination address335*/336}337338if (m->m_flags & M_FASTFWD_OURS) {339/*340* ipfw changed it for a local address on this host.341*/342goto forwardlocal;343}344345passin:346/*347* Step 4: decrement TTL and look up route348*/349350/*351* Check TTL352*/353#ifdef IPSTEALTH354if (!V_ipstealth) {355#endif356if (ip->ip_ttl <= IPTTLDEC) {357icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);358return NULL; /* mbuf already free'd */359}360361/*362* Decrement the TTL and incrementally change the IP header checksum.363* Don't bother doing this with hw checksum offloading, it's faster364* doing it right here.365*/366ip->ip_ttl -= IPTTLDEC;367if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))368ip->ip_sum -= ~htons(IPTTLDEC << 8);369else370ip->ip_sum += htons(IPTTLDEC << 8);371#ifdef IPSTEALTH372}373#endif374375/*376* Next hop forced by pfil(9) hook?377*/378if ((m->m_flags & M_IP_NEXTHOP) &&379((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {380/*381* Now we will find route to forced destination.382*/383dest.s_addr = ((struct sockaddr_in *)384(fwd_tag + 1))->sin_addr.s_addr;385m_tag_delete(m, fwd_tag);386m->m_flags &= ~M_IP_NEXTHOP;387}388389/*390* Find route to destination.391*/392if (ip_findroute(&nh, dest, m) != 0)393return (NULL); /* icmp unreach already sent */394395/*396* Avoid second route lookup by caching destination.397*/398rtdest.s_addr = dest.s_addr;399400/*401* Step 5: outgoing firewall packet processing402*/403if (!PFIL_HOOKED_OUT(V_inet_pfil_head))404goto passout;405406if (pfil_mbuf_fwd(V_inet_pfil_head, &m, nh->nh_ifp,407NULL) != PFIL_PASS)408goto drop;409410M_ASSERTVALID(m);411M_ASSERTPKTHDR(m);412413ip = mtod(m, struct ip *);414dest.s_addr = ip->ip_dst.s_addr;415416/*417* Destination address changed?418*/419if (m->m_flags & M_IP_NEXTHOP)420fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);421else422fwd_tag = NULL;423if (odest.s_addr != dest.s_addr || fwd_tag != NULL) {424/*425* Is it now for a local address on this host?426*/427if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) {428forwardlocal:429/*430* Return packet for processing by ip_input().431*/432m->m_flags |= M_FASTFWD_OURS;433return (m);434}435/*436* Redo route lookup with new destination address437*/438if (fwd_tag) {439dest.s_addr = ((struct sockaddr_in *)440(fwd_tag + 1))->sin_addr.s_addr;441m_tag_delete(m, fwd_tag);442m->m_flags &= ~M_IP_NEXTHOP;443}444if (dest.s_addr != rtdest.s_addr &&445ip_findroute(&nh, dest, m) != 0)446return (NULL); /* icmp unreach already sent */447}448449passout:450/*451* Step 6: send off the packet452*/453ip_len = ntohs(ip->ip_len);454ip_off = ntohs(ip->ip_off);455456bzero(&ro, sizeof(ro));457dst = (struct sockaddr_in *)&ro.ro_dst;458dst->sin_family = AF_INET;459dst->sin_len = sizeof(*dst);460dst->sin_addr = dest;461if (nh->nh_flags & NHF_GATEWAY) {462gw = &nh->gw_sa;463ro.ro_flags |= RT_HAS_GW;464} else465gw = (const struct sockaddr *)dst;466467/*468* If TCP/UDP header still needs a valid checksum and interface will not469* calculate it for us, do it here.470*/471if (__predict_false(m->m_pkthdr.csum_flags & CSUM_DELAY_DATA &472~nh->nh_ifp->if_hwassist)) {473in_delayed_cksum(m);474m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;475}476#if defined(SCTP) || defined(SCTP_SUPPORT)477if (__predict_false(m->m_pkthdr.csum_flags & CSUM_IP_SCTP &478~nh->nh_ifp->if_hwassist)) {479sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));480m->m_pkthdr.csum_flags &= ~CSUM_IP_SCTP;481}482#endif483484/* Handle redirect case. */485redest.s_addr = 0;486if (V_ipsendredirects && osrc.s_addr == ip->ip_src.s_addr &&487nh->nh_ifp == m->m_pkthdr.rcvif)488mcopy = ip_redir_alloc(m, nh, ip_len, &osrc, &redest);489490/*491* Check if packet fits MTU or if hardware will fragment for us492*/493if (ip_len <= nh->nh_mtu) {494/*495* Avoid confusing lower layers.496*/497m_clrprotoflags(m);498/*499* Send off the packet via outgoing interface500*/501IP_PROBE(send, NULL, NULL, ip, nh->nh_ifp, ip, NULL);502error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m, gw, &ro);503} else {504/*505* Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery506*/507if (ip_off & IP_DF) {508IPSTAT_INC(ips_cantfrag);509icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,5100, nh->nh_mtu);511goto consumed;512} else {513/*514* We have to fragment the packet515*/516m->m_pkthdr.csum_flags |= CSUM_IP;517if (ip_fragment(ip, &m, nh->nh_mtu,518nh->nh_ifp->if_hwassist) != 0)519goto drop;520KASSERT(m != NULL, ("null mbuf and no error"));521/*522* Send off the fragments via outgoing interface523*/524error = 0;525do {526m0 = m->m_nextpkt;527m->m_nextpkt = NULL;528/*529* Avoid confusing lower layers.530*/531m_clrprotoflags(m);532533IP_PROBE(send, NULL, NULL,534mtod(m, struct ip *), nh->nh_ifp,535mtod(m, struct ip *), NULL);536error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m,537gw, &ro);538if (error)539break;540} while ((m = m0) != NULL);541if (error) {542/* Reclaim remaining fragments */543for (m = m0; m; m = m0) {544m0 = m->m_nextpkt;545m_freem(m);546}547} else548IPSTAT_INC(ips_fragmented);549}550}551552if (error != 0)553IPSTAT_INC(ips_odropped);554else {555IPSTAT_INC(ips_forward);556IPSTAT_INC(ips_fastforward);557}558559/* Send required redirect */560if (mcopy != NULL) {561icmp_error(mcopy, ICMP_REDIRECT, ICMP_REDIRECT_HOST, redest.s_addr, 0);562mcopy = NULL; /* Was consumed by callee. */563}564565consumed:566if (mcopy != NULL)567m_freem(mcopy);568return NULL;569drop:570if (m)571m_freem(m);572return NULL;573}574575576