ip_fastfwd.c revision 190951
1139823Simp/*-
2122702Sandre * Copyright (c) 2003 Andre Oppermann, Internet Business Solutions AG
3122702Sandre * All rights reserved.
4122702Sandre *
5122702Sandre * Redistribution and use in source and binary forms, with or without
6122702Sandre * modification, are permitted provided that the following conditions
7122702Sandre * are met:
8122702Sandre * 1. Redistributions of source code must retain the above copyright
9122702Sandre *    notice, this list of conditions and the following disclaimer.
10122702Sandre * 2. Redistributions in binary form must reproduce the above copyright
11122702Sandre *    notice, this list of conditions and the following disclaimer in the
12122702Sandre *    documentation and/or other materials provided with the distribution.
13122702Sandre * 3. The name of the author may not be used to endorse or promote
14122702Sandre *    products derived from this software without specific prior written
15122702Sandre *    permission.
16122702Sandre *
17122702Sandre * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18122702Sandre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19122702Sandre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20122702Sandre * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21122702Sandre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22122702Sandre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23122702Sandre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24122702Sandre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25122702Sandre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26122702Sandre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27122702Sandre * SUCH DAMAGE.
28122702Sandre */
29122702Sandre
30122702Sandre/*
31122702Sandre * ip_fastforward gets its speed from processing the forwarded packet to
32122702Sandre * completion (if_output on the other side) without any queues or netisr's.
33122702Sandre * The receiving interface DMAs the packet into memory, the upper half of
34122702Sandre * driver calls ip_fastforward, we do our routing table lookup and directly
35148324Skeramida * send it off to the outgoing interface, which DMAs the packet to the
36122702Sandre * network card. The only part of the packet we touch with the CPU is the
37122759Sandre * IP header (unless there are complex firewall rules touching other parts
38122759Sandre * of the packet, but that is up to you). We are essentially limited by bus
39122759Sandre * bandwidth and how fast the network card/driver can set up receives and
40122759Sandre * transmits.
41122702Sandre *
42148324Skeramida * We handle basic errors, IP header errors, checksum errors,
43122702Sandre * destination unreachable, fragmentation and fragmentation needed and
44148324Skeramida * report them via ICMP to the sender.
45122702Sandre *
46122702Sandre * Else if something is not pure IPv4 unicast forwarding we fall back to
47122702Sandre * the normal ip_input processing path. We should only be called from
48122702Sandre * interfaces connected to the outside world.
49122702Sandre *
50122702Sandre * Firewalling is fully supported including divert, ipfw fwd and ipfilter
51122702Sandre * ipnat and address rewrite.
52122702Sandre *
53122702Sandre * IPSEC is not supported if this host is a tunnel broker. IPSEC is
54122702Sandre * supported for connections to/from local host.
55122702Sandre *
56122702Sandre * We try to do the least expensive (in CPU ops) checks and operations
57122702Sandre * first to catch junk with as little overhead as possible.
58122702Sandre *
59148324Skeramida * We take full advantage of hardware support for IP checksum and
60122702Sandre * fragmentation offloading.
61122702Sandre *
62122702Sandre * We don't do ICMP redirect in the fast forwarding path. I have had my own
63122702Sandre * cases where two core routers with Zebra routing suite would send millions
64148324Skeramida * ICMP redirects to connected hosts if the destination router was not the
65148324Skeramida * default gateway. In one case it was filling the routing table of a host
66148324Skeramida * with approximately 300.000 cloned redirect entries until it ran out of
67148324Skeramida * kernel memory. However the networking code proved very robust and it didn't
68148324Skeramida * crash or fail in other ways.
69122702Sandre */
70122702Sandre
71122702Sandre/*
72122702Sandre * Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which
73122702Sandre * is being followed here.
74122702Sandre */
75122702Sandre
76172467Ssilby#include <sys/cdefs.h>
77172467Ssilby__FBSDID("$FreeBSD: head/sys/netinet/ip_fastfwd.c 190951 2009-04-11 23:35:20Z rwatson $");
78172467Ssilby
79122702Sandre#include "opt_ipfw.h"
80122702Sandre#include "opt_ipstealth.h"
81122702Sandre
82122702Sandre#include <sys/param.h>
83122702Sandre#include <sys/systm.h>
84122702Sandre#include <sys/kernel.h>
85122702Sandre#include <sys/malloc.h>
86122702Sandre#include <sys/mbuf.h>
87122702Sandre#include <sys/protosw.h>
88122702Sandre#include <sys/socket.h>
89122702Sandre#include <sys/sysctl.h>
90181803Sbz#include <sys/vimage.h>
91122702Sandre
92122702Sandre#include <net/pfil.h>
93122702Sandre#include <net/if.h>
94122702Sandre#include <net/if_types.h>
95122702Sandre#include <net/if_var.h>
96122702Sandre#include <net/if_dl.h>
97122702Sandre#include <net/route.h>
98122702Sandre
99122702Sandre#include <netinet/in.h>
100122702Sandre#include <netinet/in_systm.h>
101122702Sandre#include <netinet/in_var.h>
102122702Sandre#include <netinet/ip.h>
103122702Sandre#include <netinet/ip_var.h>
104122702Sandre#include <netinet/ip_icmp.h>
105152592Sandre#include <netinet/ip_options.h>
106185571Sbz#include <netinet/vinet.h>
107122702Sandre
108122702Sandre#include <machine/in_cksum.h>
109122702Sandre
110185088Szec#ifdef VIMAGE_GLOBALS
111185088Szecstatic int ipfastforward_active;
112185088Szec#endif
113183550SzecSYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, fastforwarding,
114183550Szec    CTLFLAG_RW, ipfastforward_active, 0, "Enable fast IP forwarding");
115122702Sandre
116128872Sandrestatic struct sockaddr_in *
117133497Sandreip_findroute(struct route *ro, struct in_addr dest, struct mbuf *m)
118128872Sandre{
119183550Szec	INIT_VNET_INET(curvnet);
120128872Sandre	struct sockaddr_in *dst;
121128872Sandre	struct rtentry *rt;
122128872Sandre
123128872Sandre	/*
124128872Sandre	 * Find route to destination.
125128872Sandre	 */
126128872Sandre	bzero(ro, sizeof(*ro));
127128872Sandre	dst = (struct sockaddr_in *)&ro->ro_dst;
128128872Sandre	dst->sin_family = AF_INET;
129128872Sandre	dst->sin_len = sizeof(*dst);
130133497Sandre	dst->sin_addr.s_addr = dest.s_addr;
131186119Sqingli	in_rtalloc_ign(ro, 0, M_GETFIB(m));
132128872Sandre
133128872Sandre	/*
134128872Sandre	 * Route there and interface still up?
135128872Sandre	 */
136128872Sandre	rt = ro->ro_rt;
137128872Sandre	if (rt && (rt->rt_flags & RTF_UP) &&
138128872Sandre	    (rt->rt_ifp->if_flags & IFF_UP) &&
139148887Srwatson	    (rt->rt_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
140128872Sandre		if (rt->rt_flags & RTF_GATEWAY)
141128872Sandre			dst = (struct sockaddr_in *)rt->rt_gateway;
142128872Sandre	} else {
143190951Srwatson		IPSTAT_INC(ips_noroute);
144190951Srwatson		IPSTAT_INC(ips_cantforward);
145128872Sandre		if (rt)
146128872Sandre			RTFREE(rt);
147145863Sandre		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
148128872Sandre		return NULL;
149128872Sandre	}
150128872Sandre	return dst;
151128872Sandre}
152128872Sandre
153122702Sandre/*
154122702Sandre * Try to forward a packet based on the destination address.
155122702Sandre * This is a fast path optimized for the plain forwarding case.
156122702Sandre * If the packet is handled (and consumed) here then we return 1;
157122702Sandre * otherwise 0 is returned and the packet should be delivered
158122702Sandre * to ip_input for full processing.
159122702Sandre */
160154518Sandrestruct mbuf *
161122702Sandreip_fastforward(struct mbuf *m)
162122702Sandre{
163183550Szec	INIT_VNET_INET(curvnet);
164122702Sandre	struct ip *ip;
165122702Sandre	struct mbuf *m0 = NULL;
166122702Sandre	struct route ro;
167122702Sandre	struct sockaddr_in *dst = NULL;
168128872Sandre	struct ifnet *ifp;
169133497Sandre	struct in_addr odest, dest;
170128872Sandre	u_short sum, ip_len;
171122702Sandre	int error = 0;
172133920Sandre	int hlen, mtu;
173133920Sandre#ifdef IPFIREWALL_FORWARD
174133920Sandre	struct m_tag *fwd_tag;
175133920Sandre#endif
176122702Sandre
177122702Sandre	/*
178122702Sandre	 * Are we active and forwarding packets?
179122702Sandre	 */
180181803Sbz	if (!V_ipfastforward_active || !V_ipforwarding)
181154518Sandre		return m;
182122702Sandre
183122702Sandre	M_ASSERTVALID(m);
184122702Sandre	M_ASSERTPKTHDR(m);
185122702Sandre
186128872Sandre	ro.ro_rt = NULL;
187128872Sandre
188122702Sandre	/*
189122702Sandre	 * Step 1: check for packet drop conditions (and sanity checks)
190122702Sandre	 */
191122702Sandre
192122702Sandre	/*
193122702Sandre	 * Is entire packet big enough?
194122702Sandre	 */
195122702Sandre	if (m->m_pkthdr.len < sizeof(struct ip)) {
196190951Srwatson		IPSTAT_INC(ips_tooshort);
197122702Sandre		goto drop;
198122702Sandre	}
199122702Sandre
200122702Sandre	/*
201122702Sandre	 * Is first mbuf large enough for ip header and is header present?
202122702Sandre	 */
203122702Sandre	if (m->m_len < sizeof (struct ip) &&
204137302Sandre	   (m = m_pullup(m, sizeof (struct ip))) == NULL) {
205190951Srwatson		IPSTAT_INC(ips_toosmall);
206154518Sandre		return NULL;	/* mbuf already free'd */
207122702Sandre	}
208122702Sandre
209122702Sandre	ip = mtod(m, struct ip *);
210122702Sandre
211122702Sandre	/*
212122702Sandre	 * Is it IPv4?
213122702Sandre	 */
214122702Sandre	if (ip->ip_v != IPVERSION) {
215190951Srwatson		IPSTAT_INC(ips_badvers);
216122702Sandre		goto drop;
217122702Sandre	}
218122702Sandre
219122702Sandre	/*
220122702Sandre	 * Is IP header length correct and is it in first mbuf?
221122702Sandre	 */
222122702Sandre	hlen = ip->ip_hl << 2;
223122702Sandre	if (hlen < sizeof(struct ip)) {	/* minimum header length */
224190951Srwatson		IPSTAT_INC(ips_badlen);
225122702Sandre		goto drop;
226122702Sandre	}
227122702Sandre	if (hlen > m->m_len) {
228154518Sandre		if ((m = m_pullup(m, hlen)) == NULL) {
229190951Srwatson			IPSTAT_INC(ips_badhlen);
230154518Sandre			return NULL;	/* mbuf already free'd */
231122702Sandre		}
232122702Sandre		ip = mtod(m, struct ip *);
233122702Sandre	}
234122702Sandre
235122702Sandre	/*
236122702Sandre	 * Checksum correct?
237122702Sandre	 */
238122702Sandre	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED)
239122702Sandre		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
240122702Sandre	else {
241122702Sandre		if (hlen == sizeof(struct ip))
242122702Sandre			sum = in_cksum_hdr(ip);
243122702Sandre		else
244122702Sandre			sum = in_cksum(m, hlen);
245122702Sandre	}
246122702Sandre	if (sum) {
247190951Srwatson		IPSTAT_INC(ips_badsum);
248122702Sandre		goto drop;
249122702Sandre	}
250136690Sandre
251136690Sandre	/*
252148324Skeramida	 * Remember that we have checked the IP header and found it valid.
253136690Sandre	 */
254122702Sandre	m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
255122702Sandre
256128872Sandre	ip_len = ntohs(ip->ip_len);
257122702Sandre
258122702Sandre	/*
259122702Sandre	 * Is IP length longer than packet we have got?
260122702Sandre	 */
261128872Sandre	if (m->m_pkthdr.len < ip_len) {
262190951Srwatson		IPSTAT_INC(ips_tooshort);
263122702Sandre		goto drop;
264122702Sandre	}
265122702Sandre
266122702Sandre	/*
267122702Sandre	 * Is packet longer than IP header tells us? If yes, truncate packet.
268122702Sandre	 */
269128872Sandre	if (m->m_pkthdr.len > ip_len) {
270122702Sandre		if (m->m_len == m->m_pkthdr.len) {
271128872Sandre			m->m_len = ip_len;
272128872Sandre			m->m_pkthdr.len = ip_len;
273122702Sandre		} else
274128872Sandre			m_adj(m, ip_len - m->m_pkthdr.len);
275122702Sandre	}
276122702Sandre
277122702Sandre	/*
278122702Sandre	 * Is packet from or to 127/8?
279122702Sandre	 */
280122702Sandre	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
281122702Sandre	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
282190951Srwatson		IPSTAT_INC(ips_badaddr);
283122702Sandre		goto drop;
284122702Sandre	}
285122702Sandre
286133480Sandre#ifdef ALTQ
287122702Sandre	/*
288133480Sandre	 * Is packet dropped by traffic conditioner?
289133480Sandre	 */
290133480Sandre	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
291154518Sandre		goto drop;
292133480Sandre#endif
293133480Sandre
294133480Sandre	/*
295122702Sandre	 * Step 2: fallback conditions to normal ip_input path processing
296122702Sandre	 */
297122702Sandre
298122702Sandre	/*
299122702Sandre	 * Only IP packets without options
300122702Sandre	 */
301129017Sandre	if (ip->ip_hl != (sizeof(struct ip) >> 2)) {
302129017Sandre		if (ip_doopts == 1)
303154518Sandre			return m;
304129017Sandre		else if (ip_doopts == 2) {
305129017Sandre			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,
306145863Sandre				0, 0);
307154518Sandre			return NULL;	/* mbuf already free'd */
308129017Sandre		}
309129017Sandre		/* else ignore IP options and continue */
310129017Sandre	}
311122702Sandre
312122702Sandre	/*
313122702Sandre	 * Only unicast IP, not from loopback, no L2 or IP broadcast,
314122702Sandre	 * no multicast, no INADDR_ANY
315122702Sandre	 *
316122702Sandre	 * XXX: Probably some of these checks could be direct drop
317122702Sandre	 * conditions.  However it is not clear whether there are some
318122702Sandre	 * hacks or obscure behaviours which make it neccessary to
319122702Sandre	 * let ip_input handle it.  We play safe here and let ip_input
320122702Sandre	 * deal with it until it is proven that we can directly drop it.
321122702Sandre	 */
322149369Sandre	if ((m->m_flags & (M_BCAST|M_MCAST)) ||
323149369Sandre	    (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
324122702Sandre	    ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
325122702Sandre	    ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
326122702Sandre	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
327122702Sandre	    IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
328166452Sbms	    IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) ||
329166452Sbms	    IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
330149369Sandre	    ip->ip_src.s_addr == INADDR_ANY ||
331122702Sandre	    ip->ip_dst.s_addr == INADDR_ANY )
332154518Sandre		return m;
333122702Sandre
334122702Sandre	/*
335122702Sandre	 * Is it for a local address on this host?
336122702Sandre	 */
337133497Sandre	if (in_localip(ip->ip_dst))
338154518Sandre		return m;
339122702Sandre
340190951Srwatson	IPSTAT_INC(ips_total);
341122702Sandre
342122702Sandre	/*
343122702Sandre	 * Step 3: incoming packet firewall processing
344122702Sandre	 */
345122702Sandre
346128872Sandre	/*
347128872Sandre	 * Convert to host representation
348128872Sandre	 */
349128872Sandre	ip->ip_len = ntohs(ip->ip_len);
350128872Sandre	ip->ip_off = ntohs(ip->ip_off);
351128872Sandre
352133497Sandre	odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;
353134383Sandre
354122702Sandre	/*
355122702Sandre	 * Run through list of ipfilter hooks for input packets
356122702Sandre	 */
357155201Scsjp	if (!PFIL_HOOKED(&inet_pfil_hook))
358134383Sandre		goto passin;
359134383Sandre
360135920Smlaier	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL) ||
361122702Sandre	    m == NULL)
362154518Sandre		goto drop;
363122702Sandre
364122702Sandre	M_ASSERTVALID(m);
365122702Sandre	M_ASSERTPKTHDR(m);
366122702Sandre
367122702Sandre	ip = mtod(m, struct ip *);	/* m may have changed by pfil hook */
368133497Sandre	dest.s_addr = ip->ip_dst.s_addr;
369122702Sandre
370122702Sandre	/*
371122702Sandre	 * Destination address changed?
372122702Sandre	 */
373133497Sandre	if (odest.s_addr != dest.s_addr) {
374122702Sandre		/*
375122702Sandre		 * Is it now for a local address on this host?
376122702Sandre		 */
377133497Sandre		if (in_localip(dest))
378133497Sandre			goto forwardlocal;
379122702Sandre		/*
380122702Sandre		 * Go on with new destination address
381122702Sandre		 */
382122702Sandre	}
383133920Sandre#ifdef IPFIREWALL_FORWARD
384133920Sandre	if (m->m_flags & M_FASTFWD_OURS) {
385133920Sandre		/*
386133920Sandre		 * ipfw changed it for a local address on this host.
387133920Sandre		 */
388133920Sandre		goto forwardlocal;
389133920Sandre	}
390133920Sandre#endif /* IPFIREWALL_FORWARD */
391122702Sandre
392134383Sandrepassin:
393122702Sandre	/*
394122702Sandre	 * Step 4: decrement TTL and look up route
395122702Sandre	 */
396122702Sandre
397122702Sandre	/*
398122702Sandre	 * Check TTL
399122702Sandre	 */
400122702Sandre#ifdef IPSTEALTH
401181803Sbz	if (!V_ipstealth) {
402122702Sandre#endif
403122702Sandre	if (ip->ip_ttl <= IPTTLDEC) {
404145863Sandre		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
405154518Sandre		return NULL;	/* mbuf already free'd */
406122702Sandre	}
407122702Sandre
408122702Sandre	/*
409136690Sandre	 * Decrement the TTL and incrementally change the IP header checksum.
410136690Sandre	 * Don't bother doing this with hw checksum offloading, it's faster
411136690Sandre	 * doing it right here.
412122702Sandre	 */
413122702Sandre	ip->ip_ttl -= IPTTLDEC;
414122702Sandre	if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
415122702Sandre		ip->ip_sum -= ~htons(IPTTLDEC << 8);
416122702Sandre	else
417122702Sandre		ip->ip_sum += htons(IPTTLDEC << 8);
418122702Sandre#ifdef IPSTEALTH
419122702Sandre	}
420122702Sandre#endif
421122702Sandre
422122702Sandre	/*
423122702Sandre	 * Find route to destination.
424122702Sandre	 */
425128872Sandre	if ((dst = ip_findroute(&ro, dest, m)) == NULL)
426154518Sandre		return NULL;	/* icmp unreach already sent */
427128872Sandre	ifp = ro.ro_rt->rt_ifp;
428122702Sandre
429122702Sandre	/*
430166507Sbms	 * Immediately drop blackholed traffic, and directed broadcasts
431166507Sbms	 * for either the all-ones or all-zero subnet addresses on
432166507Sbms	 * locally attached networks.
433137179Sbms	 */
434166507Sbms	if ((ro.ro_rt->rt_flags & (RTF_BLACKHOLE|RTF_BROADCAST)) != 0)
435137179Sbms		goto drop;
436137179Sbms
437137179Sbms	/*
438122702Sandre	 * Step 5: outgoing firewall packet processing
439122702Sandre	 */
440122702Sandre
441122702Sandre	/*
442122702Sandre	 * Run through list of hooks for output packets.
443122702Sandre	 */
444155201Scsjp	if (!PFIL_HOOKED(&inet_pfil_hook))
445134383Sandre		goto passout;
446134383Sandre
447135920Smlaier	if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, NULL) || m == NULL) {
448154518Sandre		goto drop;
449122702Sandre	}
450122702Sandre
451122702Sandre	M_ASSERTVALID(m);
452122702Sandre	M_ASSERTPKTHDR(m);
453122702Sandre
454122702Sandre	ip = mtod(m, struct ip *);
455133497Sandre	dest.s_addr = ip->ip_dst.s_addr;
456122702Sandre
457122702Sandre	/*
458122702Sandre	 * Destination address changed?
459122702Sandre	 */
460133920Sandre#ifndef IPFIREWALL_FORWARD
461133497Sandre	if (odest.s_addr != dest.s_addr) {
462133920Sandre#else
463133920Sandre	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
464133920Sandre	if (odest.s_addr != dest.s_addr || fwd_tag != NULL) {
465133920Sandre#endif /* IPFIREWALL_FORWARD */
466122702Sandre		/*
467122702Sandre		 * Is it now for a local address on this host?
468122702Sandre		 */
469133920Sandre#ifndef IPFIREWALL_FORWARD
470133497Sandre		if (in_localip(dest)) {
471133920Sandre#else
472136690Sandre		if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) {
473133920Sandre#endif /* IPFIREWALL_FORWARD */
474122702Sandreforwardlocal:
475133497Sandre			/*
476135158Sandre			 * Return packet for processing by ip_input().
477135158Sandre			 * Keep host byte order as expected at ip_input's
478135158Sandre			 * "ours"-label.
479133497Sandre			 */
480135158Sandre			m->m_flags |= M_FASTFWD_OURS;
481133497Sandre			if (ro.ro_rt)
482133497Sandre				RTFREE(ro.ro_rt);
483154518Sandre			return m;
484122702Sandre		}
485122702Sandre		/*
486122702Sandre		 * Redo route lookup with new destination address
487122702Sandre		 */
488133920Sandre#ifdef IPFIREWALL_FORWARD
489133920Sandre		if (fwd_tag) {
490161380Sjulian			dest.s_addr = ((struct sockaddr_in *)
491157833Sglebius				    (fwd_tag + 1))->sin_addr.s_addr;
492133920Sandre			m_tag_delete(m, fwd_tag);
493133920Sandre		}
494133920Sandre#endif /* IPFIREWALL_FORWARD */
495122702Sandre		RTFREE(ro.ro_rt);
496128872Sandre		if ((dst = ip_findroute(&ro, dest, m)) == NULL)
497154518Sandre			return NULL;	/* icmp unreach already sent */
498128872Sandre		ifp = ro.ro_rt->rt_ifp;
499122702Sandre	}
500122702Sandre
501134383Sandrepassout:
502122702Sandre	/*
503122702Sandre	 * Step 6: send off the packet
504122702Sandre	 */
505122702Sandre
506122702Sandre	/*
507128872Sandre	 * Check if route is dampned (when ARP is unable to resolve)
508128872Sandre	 */
509128872Sandre	if ((ro.ro_rt->rt_flags & RTF_REJECT) &&
510167682Sbms	    (ro.ro_rt->rt_rmx.rmx_expire == 0 ||
511167682Sbms	    time_uptime < ro.ro_rt->rt_rmx.rmx_expire)) {
512145863Sandre		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
513128872Sandre		goto consumed;
514128872Sandre	}
515128872Sandre
516133480Sandre#ifndef ALTQ
517128872Sandre	/*
518128872Sandre	 * Check if there is enough space in the interface queue
519128872Sandre	 */
520128872Sandre	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
521128872Sandre	    ifp->if_snd.ifq_maxlen) {
522190951Srwatson		IPSTAT_INC(ips_odropped);
523128872Sandre		/* would send source quench here but that is depreciated */
524128872Sandre		goto drop;
525128872Sandre	}
526133480Sandre#endif
527128872Sandre
528128872Sandre	/*
529128872Sandre	 * Check if media link state of interface is not down
530128872Sandre	 */
531128872Sandre	if (ifp->if_link_state == LINK_STATE_DOWN) {
532145863Sandre		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
533128872Sandre		goto consumed;
534128872Sandre	}
535128872Sandre
536128872Sandre	/*
537148324Skeramida	 * Check if packet fits MTU or if hardware will fragment for us
538122702Sandre	 */
539122702Sandre	if (ro.ro_rt->rt_rmx.rmx_mtu)
540122702Sandre		mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu);
541122702Sandre	else
542122702Sandre		mtu = ifp->if_mtu;
543122702Sandre
544122702Sandre	if (ip->ip_len <= mtu ||
545122702Sandre	    (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) {
546122702Sandre		/*
547122702Sandre		 * Restore packet header fields to original values
548122702Sandre		 */
549122702Sandre		ip->ip_len = htons(ip->ip_len);
550122702Sandre		ip->ip_off = htons(ip->ip_off);
551122702Sandre		/*
552122702Sandre		 * Send off the packet via outgoing interface
553122702Sandre		 */
554122702Sandre		error = (*ifp->if_output)(ifp, m,
555122702Sandre				(struct sockaddr *)dst, ro.ro_rt);
556122702Sandre	} else {
557122702Sandre		/*
558128872Sandre		 * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery
559122702Sandre		 */
560122702Sandre		if (ip->ip_off & IP_DF) {
561190951Srwatson			IPSTAT_INC(ips_cantfrag);
562122702Sandre			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
563145863Sandre				0, mtu);
564128872Sandre			goto consumed;
565122702Sandre		} else {
566122702Sandre			/*
567148324Skeramida			 * We have to fragment the packet
568122702Sandre			 */
569122702Sandre			m->m_pkthdr.csum_flags |= CSUM_IP;
570128872Sandre			/*
571128872Sandre			 * ip_fragment expects ip_len and ip_off in host byte
572128872Sandre			 * order but returns all packets in network byte order
573128872Sandre			 */
574122702Sandre			if (ip_fragment(ip, &m, mtu, ifp->if_hwassist,
575122702Sandre					(~ifp->if_hwassist & CSUM_DELAY_IP))) {
576122702Sandre				goto drop;
577122702Sandre			}
578122702Sandre			KASSERT(m != NULL, ("null mbuf and no error"));
579122702Sandre			/*
580122702Sandre			 * Send off the fragments via outgoing interface
581122702Sandre			 */
582122702Sandre			error = 0;
583122702Sandre			do {
584122702Sandre				m0 = m->m_nextpkt;
585122702Sandre				m->m_nextpkt = NULL;
586122702Sandre
587122702Sandre				error = (*ifp->if_output)(ifp, m,
588122702Sandre					(struct sockaddr *)dst, ro.ro_rt);
589122702Sandre				if (error)
590122702Sandre					break;
591122702Sandre			} while ((m = m0) != NULL);
592122702Sandre			if (error) {
593122702Sandre				/* Reclaim remaining fragments */
594144301Sglebius				for (m = m0; m; m = m0) {
595122702Sandre					m0 = m->m_nextpkt;
596122702Sandre					m_freem(m);
597122702Sandre				}
598122702Sandre			} else
599190951Srwatson				IPSTAT_INC(ips_fragmented);
600122702Sandre		}
601122702Sandre	}
602122702Sandre
603122702Sandre	if (error != 0)
604190951Srwatson		IPSTAT_INC(ips_odropped);
605122702Sandre	else {
606122702Sandre		ro.ro_rt->rt_rmx.rmx_pksent++;
607190951Srwatson		IPSTAT_INC(ips_forward);
608190951Srwatson		IPSTAT_INC(ips_fastforward);
609122702Sandre	}
610128872Sandreconsumed:
611122702Sandre	RTFREE(ro.ro_rt);
612154518Sandre	return NULL;
613122702Sandredrop:
614122702Sandre	if (m)
615122702Sandre		m_freem(m);
616128872Sandre	if (ro.ro_rt)
617128872Sandre		RTFREE(ro.ro_rt);
618154518Sandre	return NULL;
619122702Sandre}
620