xref: /freebsd-src/sys/netinet/ip_fastfwd.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1c398230bSWarner Losh /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3fe267a55SPedro F. Giffuni  *
49188b4a1SAndre Oppermann  * Copyright (c) 2003 Andre Oppermann, Internet Business Solutions AG
59188b4a1SAndre Oppermann  * All rights reserved.
69188b4a1SAndre Oppermann  *
79188b4a1SAndre Oppermann  * Redistribution and use in source and binary forms, with or without
89188b4a1SAndre Oppermann  * modification, are permitted provided that the following conditions
99188b4a1SAndre Oppermann  * are met:
109188b4a1SAndre Oppermann  * 1. Redistributions of source code must retain the above copyright
119188b4a1SAndre Oppermann  *    notice, this list of conditions and the following disclaimer.
129188b4a1SAndre Oppermann  * 2. Redistributions in binary form must reproduce the above copyright
139188b4a1SAndre Oppermann  *    notice, this list of conditions and the following disclaimer in the
149188b4a1SAndre Oppermann  *    documentation and/or other materials provided with the distribution.
159188b4a1SAndre Oppermann  * 3. The name of the author may not be used to endorse or promote
169188b4a1SAndre Oppermann  *    products derived from this software without specific prior written
179188b4a1SAndre Oppermann  *    permission.
189188b4a1SAndre Oppermann  *
199188b4a1SAndre Oppermann  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
209188b4a1SAndre Oppermann  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219188b4a1SAndre Oppermann  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229188b4a1SAndre Oppermann  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
239188b4a1SAndre Oppermann  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249188b4a1SAndre Oppermann  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259188b4a1SAndre Oppermann  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269188b4a1SAndre Oppermann  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279188b4a1SAndre Oppermann  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289188b4a1SAndre Oppermann  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299188b4a1SAndre Oppermann  * SUCH DAMAGE.
309188b4a1SAndre Oppermann  */
319188b4a1SAndre Oppermann 
329188b4a1SAndre Oppermann /*
339188b4a1SAndre Oppermann  * ip_fastforward gets its speed from processing the forwarded packet to
349188b4a1SAndre Oppermann  * completion (if_output on the other side) without any queues or netisr's.
359188b4a1SAndre Oppermann  * The receiving interface DMAs the packet into memory, the upper half of
369188b4a1SAndre Oppermann  * driver calls ip_fastforward, we do our routing table lookup and directly
37a09ad793SGiorgos Keramidas  * send it off to the outgoing interface, which DMAs the packet to the
389188b4a1SAndre Oppermann  * network card. The only part of the packet we touch with the CPU is the
39df903feeSAndre Oppermann  * IP header (unless there are complex firewall rules touching other parts
40df903feeSAndre Oppermann  * of the packet, but that is up to you). We are essentially limited by bus
41df903feeSAndre Oppermann  * bandwidth and how fast the network card/driver can set up receives and
42df903feeSAndre Oppermann  * transmits.
439188b4a1SAndre Oppermann  *
44a09ad793SGiorgos Keramidas  * We handle basic errors, IP header errors, checksum errors,
459188b4a1SAndre Oppermann  * destination unreachable, fragmentation and fragmentation needed and
46a09ad793SGiorgos Keramidas  * report them via ICMP to the sender.
479188b4a1SAndre Oppermann  *
489188b4a1SAndre Oppermann  * Else if something is not pure IPv4 unicast forwarding we fall back to
499188b4a1SAndre Oppermann  * the normal ip_input processing path. We should only be called from
509188b4a1SAndre Oppermann  * interfaces connected to the outside world.
519188b4a1SAndre Oppermann  *
529188b4a1SAndre Oppermann  * Firewalling is fully supported including divert, ipfw fwd and ipfilter
539188b4a1SAndre Oppermann  * ipnat and address rewrite.
549188b4a1SAndre Oppermann  *
559188b4a1SAndre Oppermann  * IPSEC is not supported if this host is a tunnel broker. IPSEC is
569188b4a1SAndre Oppermann  * supported for connections to/from local host.
579188b4a1SAndre Oppermann  *
589188b4a1SAndre Oppermann  * We try to do the least expensive (in CPU ops) checks and operations
599188b4a1SAndre Oppermann  * first to catch junk with as little overhead as possible.
609188b4a1SAndre Oppermann  *
61a09ad793SGiorgos Keramidas  * We take full advantage of hardware support for IP checksum and
629188b4a1SAndre Oppermann  * fragmentation offloading.
639188b4a1SAndre Oppermann  */
649188b4a1SAndre Oppermann 
659188b4a1SAndre Oppermann /*
669188b4a1SAndre Oppermann  * Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which
679188b4a1SAndre Oppermann  * is being followed here.
689188b4a1SAndre Oppermann  */
699188b4a1SAndre Oppermann 
704b421e2dSMike Silbersack #include <sys/cdefs.h>
719188b4a1SAndre Oppermann #include "opt_ipstealth.h"
729188b4a1SAndre Oppermann 
739188b4a1SAndre Oppermann #include <sys/param.h>
749188b4a1SAndre Oppermann #include <sys/systm.h>
759188b4a1SAndre Oppermann #include <sys/kernel.h>
769188b4a1SAndre Oppermann #include <sys/malloc.h>
779188b4a1SAndre Oppermann #include <sys/mbuf.h>
789188b4a1SAndre Oppermann #include <sys/protosw.h>
7957f60867SMark Johnston #include <sys/sdt.h>
809188b4a1SAndre Oppermann #include <sys/socket.h>
819188b4a1SAndre Oppermann #include <sys/sysctl.h>
829188b4a1SAndre Oppermann 
839188b4a1SAndre Oppermann #include <net/if.h>
849188b4a1SAndre Oppermann #include <net/if_types.h>
859188b4a1SAndre Oppermann #include <net/if_var.h>
869188b4a1SAndre Oppermann #include <net/if_dl.h>
87*3d0d5b21SJustin Hibbits #include <net/if_private.h>
88b252313fSGleb Smirnoff #include <net/pfil.h>
899188b4a1SAndre Oppermann #include <net/route.h>
909ac7c6cfSAlexander V. Chernikov #include <net/route/nhop.h>
91530c0060SRobert Watson #include <net/vnet.h>
929188b4a1SAndre Oppermann 
939188b4a1SAndre Oppermann #include <netinet/in.h>
94dc9d21f8SAndrey V. Elsukov #include <netinet/in_fib.h>
9557f60867SMark Johnston #include <netinet/in_kdtrace.h>
969188b4a1SAndre Oppermann #include <netinet/in_systm.h>
979188b4a1SAndre Oppermann #include <netinet/in_var.h>
989188b4a1SAndre Oppermann #include <netinet/ip.h>
999188b4a1SAndre Oppermann #include <netinet/ip_var.h>
1009188b4a1SAndre Oppermann #include <netinet/ip_icmp.h>
101ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
1029188b4a1SAndre Oppermann 
1039188b4a1SAndre Oppermann #include <machine/in_cksum.h>
1049188b4a1SAndre Oppermann 
1058ad114c0SGeorge V. Neville-Neil #define	V_ipsendredirects	VNET(ipsendredirects)
1068ad114c0SGeorge V. Neville-Neil 
1078ad114c0SGeorge V. Neville-Neil static struct mbuf *
ip_redir_alloc(struct mbuf * m,struct nhop_object * nh,u_short ip_len,struct in_addr * osrc,struct in_addr * newgw)108f389439fSBjoern A. Zeeb ip_redir_alloc(struct mbuf *m, struct nhop_object *nh, u_short ip_len,
109f389439fSBjoern A. Zeeb     struct in_addr *osrc, struct in_addr *newgw)
1108ad114c0SGeorge V. Neville-Neil {
111f389439fSBjoern A. Zeeb 	struct in_ifaddr *nh_ia;
112f389439fSBjoern A. Zeeb 	struct mbuf *mcopy;
113d65d6d5aSGeorge V. Neville-Neil 
114f389439fSBjoern A. Zeeb 	KASSERT(nh != NULL, ("%s: m %p nh is NULL\n", __func__, m));
115f389439fSBjoern A. Zeeb 
116f389439fSBjoern A. Zeeb 	/*
117f389439fSBjoern A. Zeeb 	 * Only send a redirect if:
118f389439fSBjoern A. Zeeb 	 * - Redirects are not disabled (must be checked by caller),
119f389439fSBjoern A. Zeeb 	 * - We have not applied NAT (must be checked by caller as possible),
120f389439fSBjoern A. Zeeb 	 * - Neither a MCAST or BCAST packet (must be checked by caller)
121f389439fSBjoern A. Zeeb 	 *   [RFC1009 Appendix A.2].
122f389439fSBjoern A. Zeeb 	 * - The packet does not do IP source routing or having any other
123f389439fSBjoern A. Zeeb 	 *   IP options (this case was handled already by ip_input() calling
124f389439fSBjoern A. Zeeb 	 *   ip_dooptions() [RFC792, p13],
125f389439fSBjoern A. Zeeb 	 * - The packet is being forwarded out the same physical interface
126f389439fSBjoern A. Zeeb 	 *   that it was received from [RFC1812, 5.2.7.2].
127f389439fSBjoern A. Zeeb 	 */
128f389439fSBjoern A. Zeeb 
129f389439fSBjoern A. Zeeb 	/*
130f389439fSBjoern A. Zeeb 	 * - The forwarding route was not created by a redirect
131f389439fSBjoern A. Zeeb 	 *   [RFC1812, 5.2.7.2], or
132f389439fSBjoern A. Zeeb 	 *   if it was to follow a default route (see below).
133f389439fSBjoern A. Zeeb 	 * - The next-hop is reachable by us [RFC1009 Appendix A.2].
134f389439fSBjoern A. Zeeb 	 */
135f389439fSBjoern A. Zeeb 	if ((nh->nh_flags & (NHF_DEFAULT | NHF_REDIRECT |
136f389439fSBjoern A. Zeeb 	    NHF_BLACKHOLE | NHF_REJECT)) != 0)
137f389439fSBjoern A. Zeeb 		return (NULL);
138f389439fSBjoern A. Zeeb 
139f389439fSBjoern A. Zeeb 	/* Get the new gateway. */
140f389439fSBjoern A. Zeeb 	if ((nh->nh_flags & NHF_GATEWAY) == 0 || nh->gw_sa.sa_family != AF_INET)
141f389439fSBjoern A. Zeeb 		return (NULL);
142f389439fSBjoern A. Zeeb 	newgw->s_addr = nh->gw4_sa.sin_addr.s_addr;
143f389439fSBjoern A. Zeeb 
144f389439fSBjoern A. Zeeb 	/*
145f389439fSBjoern A. Zeeb 	 * - The resulting forwarding destination is not "This host on this
146f389439fSBjoern A. Zeeb 	 *   network" [RFC1122, Section 3.2.1.3] (default route check above).
147f389439fSBjoern A. Zeeb 	 */
148f389439fSBjoern A. Zeeb 	if (newgw->s_addr == 0)
149f389439fSBjoern A. Zeeb 		return (NULL);
150f389439fSBjoern A. Zeeb 
151f389439fSBjoern A. Zeeb 	/*
152f389439fSBjoern A. Zeeb 	 * - We know how to reach the sender and the source address is
153f389439fSBjoern A. Zeeb 	 *   directly connected to us [RFC792, p13].
154f389439fSBjoern A. Zeeb 	 * + The new gateway address and the source address are on the same
155f389439fSBjoern A. Zeeb 	 *   subnet [RFC1009 Appendix A.2, RFC1122 3.2.2.2, RFC1812, 5.2.7.2].
156f389439fSBjoern A. Zeeb 	 * NB: if you think multiple logical subnets on the same wire should
157f389439fSBjoern A. Zeeb 	 *     receive redirects read [RFC1812, APPENDIX C (14->15)].
158f389439fSBjoern A. Zeeb 	 */
159f389439fSBjoern A. Zeeb 	nh_ia = (struct in_ifaddr *)nh->nh_ifa;
160f389439fSBjoern A. Zeeb 	if ((ntohl(osrc->s_addr) & nh_ia->ia_subnetmask) != nh_ia->ia_subnet)
161f389439fSBjoern A. Zeeb 		return (NULL);
162f389439fSBjoern A. Zeeb 
163f389439fSBjoern A. Zeeb 	/* Prepare for sending the redirect. */
164f389439fSBjoern A. Zeeb 
165f389439fSBjoern A. Zeeb 	/*
166f389439fSBjoern A. Zeeb 	 * Make a copy of as much as we need of the packet as the original
167f389439fSBjoern A. Zeeb 	 * one will be forwarded but we need (a portion) for icmp_error().
168f389439fSBjoern A. Zeeb 	 */
169f389439fSBjoern A. Zeeb 	mcopy = m_gethdr(M_NOWAIT, m->m_type);
170d65d6d5aSGeorge V. Neville-Neil 	if (mcopy == NULL)
171d65d6d5aSGeorge V. Neville-Neil 		return (NULL);
172d65d6d5aSGeorge V. Neville-Neil 
173d65d6d5aSGeorge V. Neville-Neil 	if (m_dup_pkthdr(mcopy, m, M_NOWAIT) == 0) {
1748ad114c0SGeorge V. Neville-Neil 		/*
1758ad114c0SGeorge V. Neville-Neil 		 * It's probably ok if the pkthdr dup fails (because
1768ad114c0SGeorge V. Neville-Neil 		 * the deep copy of the tag chain failed), but for now
1778ad114c0SGeorge V. Neville-Neil 		 * be conservative and just discard the copy since
1788ad114c0SGeorge V. Neville-Neil 		 * code below may some day want the tags.
1798ad114c0SGeorge V. Neville-Neil 		 */
1808ad114c0SGeorge V. Neville-Neil 		m_free(mcopy);
1818ad114c0SGeorge V. Neville-Neil 		return (NULL);
1828ad114c0SGeorge V. Neville-Neil 	}
183f389439fSBjoern A. Zeeb 	mcopy->m_len = min(ip_len, M_TRAILINGSPACE(mcopy));
1848ad114c0SGeorge V. Neville-Neil 	mcopy->m_pkthdr.len = mcopy->m_len;
1858ad114c0SGeorge V. Neville-Neil 	m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1868ad114c0SGeorge V. Neville-Neil 
1878ad114c0SGeorge V. Neville-Neil 	return (mcopy);
1888ad114c0SGeorge V. Neville-Neil }
1898ad114c0SGeorge V. Neville-Neil 
1908ad114c0SGeorge V. Neville-Neil 
191dc9d21f8SAndrey V. Elsukov static int
ip_findroute(struct nhop_object ** pnh,struct in_addr dest,struct mbuf * m)1929ac7c6cfSAlexander V. Chernikov ip_findroute(struct nhop_object **pnh, struct in_addr dest, struct mbuf *m)
19306bb56f4SAndre Oppermann {
1949ac7c6cfSAlexander V. Chernikov 	struct nhop_object *nh;
19506bb56f4SAndre Oppermann 
1969ac7c6cfSAlexander V. Chernikov 	nh = fib4_lookup(M_GETFIB(m), dest, 0, NHR_NONE,
1979ac7c6cfSAlexander V. Chernikov 	    m->m_pkthdr.flowid);
1989ac7c6cfSAlexander V. Chernikov 	if (nh == NULL) {
19986425c62SRobert Watson 		IPSTAT_INC(ips_noroute);
20086425c62SRobert Watson 		IPSTAT_INC(ips_cantforward);
201c773494eSAndre Oppermann 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
202dc9d21f8SAndrey V. Elsukov 		return (EHOSTUNREACH);
20306bb56f4SAndre Oppermann 	}
204dc9d21f8SAndrey V. Elsukov 	/*
205dc9d21f8SAndrey V. Elsukov 	 * Drop blackholed traffic and directed broadcasts.
206dc9d21f8SAndrey V. Elsukov 	 */
2079ac7c6cfSAlexander V. Chernikov 	if ((nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) != 0) {
208dc9d21f8SAndrey V. Elsukov 		IPSTAT_INC(ips_cantforward);
209dc9d21f8SAndrey V. Elsukov 		m_freem(m);
210dc9d21f8SAndrey V. Elsukov 		return (EHOSTUNREACH);
211dc9d21f8SAndrey V. Elsukov 	}
212dc9d21f8SAndrey V. Elsukov 
2139ac7c6cfSAlexander V. Chernikov 	if (nh->nh_flags & NHF_REJECT) {
214dc9d21f8SAndrey V. Elsukov 		IPSTAT_INC(ips_cantforward);
215dc9d21f8SAndrey V. Elsukov 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
216dc9d21f8SAndrey V. Elsukov 		return (EHOSTUNREACH);
217dc9d21f8SAndrey V. Elsukov 	}
218dc9d21f8SAndrey V. Elsukov 
2199ac7c6cfSAlexander V. Chernikov 	*pnh = nh;
2209ac7c6cfSAlexander V. Chernikov 
221dc9d21f8SAndrey V. Elsukov 	return (0);
22206bb56f4SAndre Oppermann }
22306bb56f4SAndre Oppermann 
2249188b4a1SAndre Oppermann /*
2259188b4a1SAndre Oppermann  * Try to forward a packet based on the destination address.
2269188b4a1SAndre Oppermann  * This is a fast path optimized for the plain forwarding case.
22736048841SPawel Jakub Dawidek  * If the packet is handled (and consumed) here then we return NULL;
22836048841SPawel Jakub Dawidek  * otherwise mbuf is returned and the packet should be delivered
2299188b4a1SAndre Oppermann  * to ip_input for full processing.
2309188b4a1SAndre Oppermann  */
2315d691e6dSAndre Oppermann struct mbuf *
ip_tryforward(struct mbuf * m)23233872124SGeorge V. Neville-Neil ip_tryforward(struct mbuf *m)
2339188b4a1SAndre Oppermann {
2349188b4a1SAndre Oppermann 	struct ip *ip;
2359188b4a1SAndre Oppermann 	struct mbuf *m0 = NULL;
2368ad114c0SGeorge V. Neville-Neil 	struct nhop_object *nh = NULL;
23762e1a437SZhenlei Huang 	struct route ro;
23862e1a437SZhenlei Huang 	struct sockaddr_in *dst;
23962e1a437SZhenlei Huang 	const struct sockaddr *gw;
240f389439fSBjoern A. Zeeb 	struct in_addr dest, odest, rtdest, osrc;
24133872124SGeorge V. Neville-Neil 	uint16_t ip_len, ip_off;
2429188b4a1SAndre Oppermann 	int error = 0;
243c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
2448ad114c0SGeorge V. Neville-Neil 	struct mbuf *mcopy = NULL;
2458ad114c0SGeorge V. Neville-Neil 	struct in_addr redest;
2469188b4a1SAndre Oppermann 	/*
2479188b4a1SAndre Oppermann 	 * Are we active and forwarding packets?
2489188b4a1SAndre Oppermann 	 */
2499188b4a1SAndre Oppermann 
2509188b4a1SAndre Oppermann 	M_ASSERTVALID(m);
2519188b4a1SAndre Oppermann 	M_ASSERTPKTHDR(m);
2529188b4a1SAndre Oppermann 
2539188b4a1SAndre Oppermann 	/*
2549188b4a1SAndre Oppermann 	 * Only IP packets without options
2559188b4a1SAndre Oppermann 	 */
25633872124SGeorge V. Neville-Neil 	ip = mtod(m, struct ip *);
25733872124SGeorge V. Neville-Neil 
2582bde81acSAndre Oppermann 	if (ip->ip_hl != (sizeof(struct ip) >> 2)) {
259348aae23SHiroki Sato 		if (V_ip_doopts == 1)
2605d691e6dSAndre Oppermann 			return m;
261348aae23SHiroki Sato 		else if (V_ip_doopts == 2) {
2622bde81acSAndre Oppermann 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,
263c773494eSAndre Oppermann 				0, 0);
2645d691e6dSAndre Oppermann 			return NULL;	/* mbuf already free'd */
2652bde81acSAndre Oppermann 		}
2662bde81acSAndre Oppermann 		/* else ignore IP options and continue */
2672bde81acSAndre Oppermann 	}
2689188b4a1SAndre Oppermann 
2699188b4a1SAndre Oppermann 	/*
2709188b4a1SAndre Oppermann 	 * Only unicast IP, not from loopback, no L2 or IP broadcast,
2719188b4a1SAndre Oppermann 	 * no multicast, no INADDR_ANY
2729188b4a1SAndre Oppermann 	 *
2739188b4a1SAndre Oppermann 	 * XXX: Probably some of these checks could be direct drop
2749188b4a1SAndre Oppermann 	 * conditions.  However it is not clear whether there are some
275a4641f4eSPedro F. Giffuni 	 * hacks or obscure behaviours which make it necessary to
2769188b4a1SAndre Oppermann 	 * let ip_input handle it.  We play safe here and let ip_input
2779188b4a1SAndre Oppermann 	 * deal with it until it is proven that we can directly drop it.
2789188b4a1SAndre Oppermann 	 */
279d56ea155SAndre Oppermann 	if ((m->m_flags & (M_BCAST|M_MCAST)) ||
280d56ea155SAndre Oppermann 	    (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
2819188b4a1SAndre Oppermann 	    ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
2829188b4a1SAndre Oppermann 	    ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
2839188b4a1SAndre Oppermann 	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
2849188b4a1SAndre Oppermann 	    IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
285d256723bSBruce M Simpson 	    IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) ||
286d256723bSBruce M Simpson 	    IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
287d56ea155SAndre Oppermann 	    ip->ip_src.s_addr == INADDR_ANY ||
2889188b4a1SAndre Oppermann 	    ip->ip_dst.s_addr == INADDR_ANY )
2895d691e6dSAndre Oppermann 		return m;
2909188b4a1SAndre Oppermann 
2919188b4a1SAndre Oppermann 	/*
2929188b4a1SAndre Oppermann 	 * Is it for a local address on this host?
2939188b4a1SAndre Oppermann 	 */
29467d0b24eSAndre Oppermann 	if (in_localip(ip->ip_dst))
2955d691e6dSAndre Oppermann 		return m;
2969188b4a1SAndre Oppermann 
29786425c62SRobert Watson 	IPSTAT_INC(ips_total);
2989188b4a1SAndre Oppermann 
2999188b4a1SAndre Oppermann 	/*
3009188b4a1SAndre Oppermann 	 * Step 3: incoming packet firewall processing
3019188b4a1SAndre Oppermann 	 */
3029188b4a1SAndre Oppermann 
30367d0b24eSAndre Oppermann 	odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;
304f389439fSBjoern A. Zeeb 	osrc.s_addr = ip->ip_src.s_addr;
305c21fd232SAndre Oppermann 
3069188b4a1SAndre Oppermann 	/*
3079188b4a1SAndre Oppermann 	 * Run through list of ipfilter hooks for input packets
3089188b4a1SAndre Oppermann 	 */
309b252313fSGleb Smirnoff 	if (!PFIL_HOOKED_IN(V_inet_pfil_head))
310c21fd232SAndre Oppermann 		goto passin;
311c21fd232SAndre Oppermann 
312dda6376bSMateusz Guzik 	if (pfil_mbuf_in(V_inet_pfil_head, &m, m->m_pkthdr.rcvif,
313b252313fSGleb Smirnoff 	    NULL) != PFIL_PASS)
3145d691e6dSAndre Oppermann 		goto drop;
3159188b4a1SAndre Oppermann 
3169188b4a1SAndre Oppermann 	M_ASSERTVALID(m);
3179188b4a1SAndre Oppermann 	M_ASSERTPKTHDR(m);
3189188b4a1SAndre Oppermann 
3199188b4a1SAndre Oppermann 	ip = mtod(m, struct ip *);	/* m may have changed by pfil hook */
32067d0b24eSAndre Oppermann 	dest.s_addr = ip->ip_dst.s_addr;
3219188b4a1SAndre Oppermann 
3229188b4a1SAndre Oppermann 	/*
3239188b4a1SAndre Oppermann 	 * Destination address changed?
3249188b4a1SAndre Oppermann 	 */
32567d0b24eSAndre Oppermann 	if (odest.s_addr != dest.s_addr) {
3269188b4a1SAndre Oppermann 		/*
3279188b4a1SAndre Oppermann 		 * Is it now for a local address on this host?
3289188b4a1SAndre Oppermann 		 */
32967d0b24eSAndre Oppermann 		if (in_localip(dest))
3309188b4a1SAndre Oppermann 			goto forwardlocal;
3319188b4a1SAndre Oppermann 		/*
3329188b4a1SAndre Oppermann 		 * Go on with new destination address
3339188b4a1SAndre Oppermann 		 */
3349188b4a1SAndre Oppermann 	}
335c1de64a4SAndrey V. Elsukov 
3369b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
3379b932e9eSAndre Oppermann 		/*
3389b932e9eSAndre Oppermann 		 * ipfw changed it for a local address on this host.
3399b932e9eSAndre Oppermann 		 */
3409b932e9eSAndre Oppermann 		goto forwardlocal;
3419b932e9eSAndre Oppermann 	}
3429188b4a1SAndre Oppermann 
343c21fd232SAndre Oppermann passin:
3449188b4a1SAndre Oppermann 	/*
3459188b4a1SAndre Oppermann 	 * Step 4: decrement TTL and look up route
3469188b4a1SAndre Oppermann 	 */
3479188b4a1SAndre Oppermann 
3489188b4a1SAndre Oppermann 	/*
3499188b4a1SAndre Oppermann 	 * Check TTL
3509188b4a1SAndre Oppermann 	 */
3519188b4a1SAndre Oppermann #ifdef IPSTEALTH
352603724d3SBjoern A. Zeeb 	if (!V_ipstealth) {
3539188b4a1SAndre Oppermann #endif
3549188b4a1SAndre Oppermann 	if (ip->ip_ttl <= IPTTLDEC) {
355c773494eSAndre Oppermann 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
3565d691e6dSAndre Oppermann 		return NULL;	/* mbuf already free'd */
3579188b4a1SAndre Oppermann 	}
3589188b4a1SAndre Oppermann 
3599188b4a1SAndre Oppermann 	/*
360de1c2ac4SAndre Oppermann 	 * Decrement the TTL and incrementally change the IP header checksum.
361de1c2ac4SAndre Oppermann 	 * Don't bother doing this with hw checksum offloading, it's faster
362de1c2ac4SAndre Oppermann 	 * doing it right here.
3639188b4a1SAndre Oppermann 	 */
3649188b4a1SAndre Oppermann 	ip->ip_ttl -= IPTTLDEC;
3659188b4a1SAndre Oppermann 	if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
3669188b4a1SAndre Oppermann 		ip->ip_sum -= ~htons(IPTTLDEC << 8);
3679188b4a1SAndre Oppermann 	else
3689188b4a1SAndre Oppermann 		ip->ip_sum += htons(IPTTLDEC << 8);
3699188b4a1SAndre Oppermann #ifdef IPSTEALTH
3709188b4a1SAndre Oppermann 	}
3719188b4a1SAndre Oppermann #endif
3729188b4a1SAndre Oppermann 
3739188b4a1SAndre Oppermann 	/*
374d5d21ad9SEugene Grosbein 	 * Next hop forced by pfil(9) hook?
375d5d21ad9SEugene Grosbein 	 */
376d5d21ad9SEugene Grosbein 	if ((m->m_flags & M_IP_NEXTHOP) &&
377d5d21ad9SEugene Grosbein 	    ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {
378d5d21ad9SEugene Grosbein 		/*
379d5d21ad9SEugene Grosbein 		 * Now we will find route to forced destination.
380d5d21ad9SEugene Grosbein 		 */
381d5d21ad9SEugene Grosbein 		dest.s_addr = ((struct sockaddr_in *)
382d5d21ad9SEugene Grosbein 			    (fwd_tag + 1))->sin_addr.s_addr;
383d5d21ad9SEugene Grosbein 		m_tag_delete(m, fwd_tag);
384d5d21ad9SEugene Grosbein 		m->m_flags &= ~M_IP_NEXTHOP;
385d5d21ad9SEugene Grosbein 	}
386d5d21ad9SEugene Grosbein 
387d5d21ad9SEugene Grosbein 	/*
3889188b4a1SAndre Oppermann 	 * Find route to destination.
3899188b4a1SAndre Oppermann 	 */
390dc9d21f8SAndrey V. Elsukov 	if (ip_findroute(&nh, dest, m) != 0)
391dc9d21f8SAndrey V. Elsukov 		return (NULL);	/* icmp unreach already sent */
39238f06105SBruce M Simpson 
39338f06105SBruce M Simpson 	/*
394d5d21ad9SEugene Grosbein 	 * Avoid second route lookup by caching destination.
395d5d21ad9SEugene Grosbein 	 */
396d5d21ad9SEugene Grosbein 	rtdest.s_addr = dest.s_addr;
397d5d21ad9SEugene Grosbein 
398d5d21ad9SEugene Grosbein 	/*
3999188b4a1SAndre Oppermann 	 * Step 5: outgoing firewall packet processing
4009188b4a1SAndre Oppermann 	 */
401b252313fSGleb Smirnoff 	if (!PFIL_HOOKED_OUT(V_inet_pfil_head))
402c21fd232SAndre Oppermann 		goto passout;
403c21fd232SAndre Oppermann 
404dda6376bSMateusz Guzik 	if (pfil_mbuf_out(V_inet_pfil_head, &m, nh->nh_ifp,
405dda6376bSMateusz Guzik 	    NULL) != PFIL_PASS)
4065d691e6dSAndre Oppermann 		goto drop;
4079188b4a1SAndre Oppermann 
4089188b4a1SAndre Oppermann 	M_ASSERTVALID(m);
4099188b4a1SAndre Oppermann 	M_ASSERTPKTHDR(m);
4109188b4a1SAndre Oppermann 
4119188b4a1SAndre Oppermann 	ip = mtod(m, struct ip *);
41267d0b24eSAndre Oppermann 	dest.s_addr = ip->ip_dst.s_addr;
4139188b4a1SAndre Oppermann 
4149188b4a1SAndre Oppermann 	/*
4159188b4a1SAndre Oppermann 	 * Destination address changed?
4169188b4a1SAndre Oppermann 	 */
417ffdbf9daSAndrey V. Elsukov 	if (m->m_flags & M_IP_NEXTHOP)
4189b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
419d5d21ad9SEugene Grosbein 	else
420d5d21ad9SEugene Grosbein 		fwd_tag = NULL;
4219b932e9eSAndre Oppermann 	if (odest.s_addr != dest.s_addr || fwd_tag != NULL) {
4229188b4a1SAndre Oppermann 		/*
4239188b4a1SAndre Oppermann 		 * Is it now for a local address on this host?
4249188b4a1SAndre Oppermann 		 */
425de1c2ac4SAndre Oppermann 		if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) {
4269188b4a1SAndre Oppermann forwardlocal:
4279188b4a1SAndre Oppermann 			/*
428eedc0a75SAndre Oppermann 			 * Return packet for processing by ip_input().
4299188b4a1SAndre Oppermann 			 */
430eedc0a75SAndre Oppermann 			m->m_flags |= M_FASTFWD_OURS;
431dc9d21f8SAndrey V. Elsukov 			return (m);
4329188b4a1SAndre Oppermann 		}
4339188b4a1SAndre Oppermann 		/*
4349188b4a1SAndre Oppermann 		 * Redo route lookup with new destination address
4359188b4a1SAndre Oppermann 		 */
4369b932e9eSAndre Oppermann 		if (fwd_tag) {
4374cbb1185SGleb Smirnoff 			dest.s_addr = ((struct sockaddr_in *)
4384cbb1185SGleb Smirnoff 				    (fwd_tag + 1))->sin_addr.s_addr;
4399b932e9eSAndre Oppermann 			m_tag_delete(m, fwd_tag);
440ffdbf9daSAndrey V. Elsukov 			m->m_flags &= ~M_IP_NEXTHOP;
4419b932e9eSAndre Oppermann 		}
442d5d21ad9SEugene Grosbein 		if (dest.s_addr != rtdest.s_addr &&
443d5d21ad9SEugene Grosbein 		    ip_findroute(&nh, dest, m) != 0)
444dc9d21f8SAndrey V. Elsukov 			return (NULL);	/* icmp unreach already sent */
4459188b4a1SAndre Oppermann 	}
4469188b4a1SAndre Oppermann 
447c21fd232SAndre Oppermann passout:
4489188b4a1SAndre Oppermann 	/*
4499188b4a1SAndre Oppermann 	 * Step 6: send off the packet
4509188b4a1SAndre Oppermann 	 */
45121d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
45221d172a3SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
4539188b4a1SAndre Oppermann 
45462e1a437SZhenlei Huang 	bzero(&ro, sizeof(ro));
45562e1a437SZhenlei Huang 	dst = (struct sockaddr_in *)&ro.ro_dst;
45662e1a437SZhenlei Huang 	dst->sin_family = AF_INET;
45762e1a437SZhenlei Huang 	dst->sin_len = sizeof(*dst);
45862e1a437SZhenlei Huang 	dst->sin_addr = dest;
45962e1a437SZhenlei Huang 	if (nh->nh_flags & NHF_GATEWAY) {
46062e1a437SZhenlei Huang 		gw = &nh->gw_sa;
46162e1a437SZhenlei Huang 		ro.ro_flags |= RT_HAS_GW;
46262e1a437SZhenlei Huang 	} else
46362e1a437SZhenlei Huang 		gw = (const struct sockaddr *)dst;
46406bb56f4SAndre Oppermann 
465f389439fSBjoern A. Zeeb 	/* Handle redirect case. */
4668ad114c0SGeorge V. Neville-Neil 	redest.s_addr = 0;
467f389439fSBjoern A. Zeeb 	if (V_ipsendredirects && osrc.s_addr == ip->ip_src.s_addr &&
468f389439fSBjoern A. Zeeb 	    nh->nh_ifp == m->m_pkthdr.rcvif)
469f389439fSBjoern A. Zeeb 		mcopy = ip_redir_alloc(m, nh, ip_len, &osrc, &redest);
4708ad114c0SGeorge V. Neville-Neil 
4718ad114c0SGeorge V. Neville-Neil 	/*
472a09ad793SGiorgos Keramidas 	 * Check if packet fits MTU or if hardware will fragment for us
4739188b4a1SAndre Oppermann 	 */
4749ac7c6cfSAlexander V. Chernikov 	if (ip_len <= nh->nh_mtu) {
4759188b4a1SAndre Oppermann 		/*
47686bd0491SAndre Oppermann 		 * Avoid confusing lower layers.
47786bd0491SAndre Oppermann 		 */
47886bd0491SAndre Oppermann 		m_clrprotoflags(m);
47986bd0491SAndre Oppermann 		/*
4809188b4a1SAndre Oppermann 		 * Send off the packet via outgoing interface
4819188b4a1SAndre Oppermann 		 */
4829ac7c6cfSAlexander V. Chernikov 		IP_PROBE(send, NULL, NULL, ip, nh->nh_ifp, ip, NULL);
48362e1a437SZhenlei Huang 		error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m, gw, &ro);
4849188b4a1SAndre Oppermann 	} else {
4859188b4a1SAndre Oppermann 		/*
48606bb56f4SAndre Oppermann 		 * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery
4879188b4a1SAndre Oppermann 		 */
48821d172a3SGleb Smirnoff 		if (ip_off & IP_DF) {
48986425c62SRobert Watson 			IPSTAT_INC(ips_cantfrag);
4909188b4a1SAndre Oppermann 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
4919ac7c6cfSAlexander V. Chernikov 				0, nh->nh_mtu);
49206bb56f4SAndre Oppermann 			goto consumed;
4939188b4a1SAndre Oppermann 		} else {
4949188b4a1SAndre Oppermann 			/*
495a09ad793SGiorgos Keramidas 			 * We have to fragment the packet
4969188b4a1SAndre Oppermann 			 */
4979188b4a1SAndre Oppermann 			m->m_pkthdr.csum_flags |= CSUM_IP;
4989ac7c6cfSAlexander V. Chernikov 			if (ip_fragment(ip, &m, nh->nh_mtu,
4999ac7c6cfSAlexander V. Chernikov 			    nh->nh_ifp->if_hwassist) != 0)
5009188b4a1SAndre Oppermann 				goto drop;
5019188b4a1SAndre Oppermann 			KASSERT(m != NULL, ("null mbuf and no error"));
5029188b4a1SAndre Oppermann 			/*
5039188b4a1SAndre Oppermann 			 * Send off the fragments via outgoing interface
5049188b4a1SAndre Oppermann 			 */
5059188b4a1SAndre Oppermann 			error = 0;
5069188b4a1SAndre Oppermann 			do {
5079188b4a1SAndre Oppermann 				m0 = m->m_nextpkt;
5089188b4a1SAndre Oppermann 				m->m_nextpkt = NULL;
50986bd0491SAndre Oppermann 				/*
51086bd0491SAndre Oppermann 				 * Avoid confusing lower layers.
51186bd0491SAndre Oppermann 				 */
51286bd0491SAndre Oppermann 				m_clrprotoflags(m);
5139188b4a1SAndre Oppermann 
5142e77d270SAndrey V. Elsukov 				IP_PROBE(send, NULL, NULL,
5159ac7c6cfSAlexander V. Chernikov 				    mtod(m, struct ip *), nh->nh_ifp,
5162e77d270SAndrey V. Elsukov 				    mtod(m, struct ip *), NULL);
5179ac7c6cfSAlexander V. Chernikov 				error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m,
51862e1a437SZhenlei Huang 				    gw, &ro);
5199188b4a1SAndre Oppermann 				if (error)
5209188b4a1SAndre Oppermann 					break;
5219188b4a1SAndre Oppermann 			} while ((m = m0) != NULL);
5229188b4a1SAndre Oppermann 			if (error) {
5239188b4a1SAndre Oppermann 				/* Reclaim remaining fragments */
524d1a47429SGleb Smirnoff 				for (m = m0; m; m = m0) {
5259188b4a1SAndre Oppermann 					m0 = m->m_nextpkt;
5269188b4a1SAndre Oppermann 					m_freem(m);
5279188b4a1SAndre Oppermann 				}
5289188b4a1SAndre Oppermann 			} else
52986425c62SRobert Watson 				IPSTAT_INC(ips_fragmented);
5309188b4a1SAndre Oppermann 		}
5319188b4a1SAndre Oppermann 	}
5329188b4a1SAndre Oppermann 
5339188b4a1SAndre Oppermann 	if (error != 0)
53486425c62SRobert Watson 		IPSTAT_INC(ips_odropped);
5359188b4a1SAndre Oppermann 	else {
53686425c62SRobert Watson 		IPSTAT_INC(ips_forward);
53786425c62SRobert Watson 		IPSTAT_INC(ips_fastforward);
5389188b4a1SAndre Oppermann 	}
5398ad114c0SGeorge V. Neville-Neil 
5408ad114c0SGeorge V. Neville-Neil 	/* Send required redirect */
5418ad114c0SGeorge V. Neville-Neil 	if (mcopy != NULL) {
5428ad114c0SGeorge V. Neville-Neil 		icmp_error(mcopy, ICMP_REDIRECT, ICMP_REDIRECT_HOST, redest.s_addr, 0);
543f389439fSBjoern A. Zeeb 		mcopy = NULL; /* Was consumed by callee. */
5448ad114c0SGeorge V. Neville-Neil 	}
5458ad114c0SGeorge V. Neville-Neil 
54606bb56f4SAndre Oppermann consumed:
5478ad114c0SGeorge V. Neville-Neil 	if (mcopy != NULL)
5488ad114c0SGeorge V. Neville-Neil 		m_freem(mcopy);
5495d691e6dSAndre Oppermann 	return NULL;
5509188b4a1SAndre Oppermann drop:
5519188b4a1SAndre Oppermann 	if (m)
5529188b4a1SAndre Oppermann 		m_freem(m);
5535d691e6dSAndre Oppermann 	return NULL;
5549188b4a1SAndre Oppermann }
555