ip_fastfwd.c revision 135158
1122702Sandre/*
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 * $FreeBSD: head/sys/netinet/ip_fastfwd.c 135158 2004-09-13 17:01:53Z andre $
30122702Sandre */
31122702Sandre
32122702Sandre/*
33122702Sandre * ip_fastforward gets its speed from processing the forwarded packet to
34122702Sandre * completion (if_output on the other side) without any queues or netisr's.
35122702Sandre * The receiving interface DMAs the packet into memory, the upper half of
36122702Sandre * driver calls ip_fastforward, we do our routing table lookup and directly
37122702Sandre * send it off to the outgoing interface which DMAs the packet to the
38122702Sandre * network card. The only part of the packet we touch with the CPU is the
39122759Sandre * IP header (unless there are complex firewall rules touching other parts
40122759Sandre * of the packet, but that is up to you). We are essentially limited by bus
41122759Sandre * bandwidth and how fast the network card/driver can set up receives and
42122759Sandre * transmits.
43122702Sandre *
44122702Sandre * We handle basic errors, ip header errors, checksum errors,
45122702Sandre * destination unreachable, fragmentation and fragmentation needed and
46122702Sandre * report them via icmp to the sender.
47122702Sandre *
48122702Sandre * Else if something is not pure IPv4 unicast forwarding we fall back to
49122702Sandre * the normal ip_input processing path. We should only be called from
50122702Sandre * interfaces connected to the outside world.
51122702Sandre *
52122702Sandre * Firewalling is fully supported including divert, ipfw fwd and ipfilter
53122702Sandre * ipnat and address rewrite.
54122702Sandre *
55122702Sandre * IPSEC is not supported if this host is a tunnel broker. IPSEC is
56122702Sandre * supported for connections to/from local host.
57122702Sandre *
58122702Sandre * We try to do the least expensive (in CPU ops) checks and operations
59122702Sandre * first to catch junk with as little overhead as possible.
60122702Sandre *
61122702Sandre * We take full advantage of hardware support for ip checksum and
62122702Sandre * fragmentation offloading.
63122702Sandre *
64122702Sandre * We don't do ICMP redirect in the fast forwarding path. I have had my own
65122702Sandre * cases where two core routers with Zebra routing suite would send millions
66122702Sandre * ICMP redirects to connected hosts if the router to dest was not the default
67122702Sandre * gateway. In one case it was filling the routing table of a host with close
68122702Sandre * 300'000 cloned redirect entries until it ran out of kernel memory. However
69122702Sandre * the networking code proved very robust and it didn't crash or went ill
70122702Sandre * otherwise.
71122702Sandre */
72122702Sandre
73122702Sandre/*
74122702Sandre * Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which
75122702Sandre * is being followed here.
76122702Sandre */
77122702Sandre
78122702Sandre#include "opt_ipfw.h"
79122702Sandre#include "opt_ipstealth.h"
80122702Sandre
81122702Sandre#include <sys/param.h>
82122702Sandre#include <sys/systm.h>
83122702Sandre#include <sys/kernel.h>
84122702Sandre#include <sys/malloc.h>
85122702Sandre#include <sys/mbuf.h>
86122702Sandre#include <sys/protosw.h>
87122702Sandre#include <sys/socket.h>
88122702Sandre#include <sys/sysctl.h>
89122702Sandre
90122702Sandre#include <net/pfil.h>
91122702Sandre#include <net/if.h>
92122702Sandre#include <net/if_types.h>
93122702Sandre#include <net/if_var.h>
94122702Sandre#include <net/if_dl.h>
95122702Sandre#include <net/route.h>
96122702Sandre
97122702Sandre#include <netinet/in.h>
98122702Sandre#include <netinet/in_systm.h>
99122702Sandre#include <netinet/in_var.h>
100122702Sandre#include <netinet/ip.h>
101122702Sandre#include <netinet/ip_var.h>
102122702Sandre#include <netinet/ip_icmp.h>
103122702Sandre
104122702Sandre#include <machine/in_cksum.h>
105122702Sandre
106122702Sandrestatic int ipfastforward_active = 0;
107122702SandreSYSCTL_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW,
108122702Sandre    &ipfastforward_active, 0, "Enable fast IP forwarding");
109122702Sandre
110128872Sandrestatic struct sockaddr_in *
111133497Sandreip_findroute(struct route *ro, struct in_addr dest, struct mbuf *m)
112128872Sandre{
113128872Sandre	struct sockaddr_in *dst;
114128872Sandre	struct rtentry *rt;
115128872Sandre
116128872Sandre	/*
117128872Sandre	 * Find route to destination.
118128872Sandre	 */
119128872Sandre	bzero(ro, sizeof(*ro));
120128872Sandre	dst = (struct sockaddr_in *)&ro->ro_dst;
121128872Sandre	dst->sin_family = AF_INET;
122128872Sandre	dst->sin_len = sizeof(*dst);
123133497Sandre	dst->sin_addr.s_addr = dest.s_addr;
124128872Sandre	rtalloc_ign(ro, RTF_CLONING);
125128872Sandre
126128872Sandre	/*
127128872Sandre	 * Route there and interface still up?
128128872Sandre	 */
129128872Sandre	rt = ro->ro_rt;
130128872Sandre	if (rt && (rt->rt_flags & RTF_UP) &&
131128872Sandre	    (rt->rt_ifp->if_flags & IFF_UP) &&
132128872Sandre	    (rt->rt_ifp->if_flags & IFF_RUNNING)) {
133128872Sandre		if (rt->rt_flags & RTF_GATEWAY)
134128872Sandre			dst = (struct sockaddr_in *)rt->rt_gateway;
135128872Sandre	} else {
136128872Sandre		ipstat.ips_noroute++;
137128872Sandre		ipstat.ips_cantforward++;
138128872Sandre		if (rt)
139128872Sandre			RTFREE(rt);
140128872Sandre		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL);
141128872Sandre		return NULL;
142128872Sandre	}
143128872Sandre	return dst;
144128872Sandre}
145128872Sandre
146122702Sandre/*
147122702Sandre * Try to forward a packet based on the destination address.
148122702Sandre * This is a fast path optimized for the plain forwarding case.
149122702Sandre * If the packet is handled (and consumed) here then we return 1;
150122702Sandre * otherwise 0 is returned and the packet should be delivered
151122702Sandre * to ip_input for full processing.
152122702Sandre */
153122702Sandreint
154122702Sandreip_fastforward(struct mbuf *m)
155122702Sandre{
156122702Sandre	struct ip *ip;
157122702Sandre	struct mbuf *m0 = NULL;
158122702Sandre	struct route ro;
159122702Sandre	struct sockaddr_in *dst = NULL;
160122702Sandre	struct in_ifaddr *ia = NULL;
161122702Sandre	struct ifaddr *ifa = NULL;
162128872Sandre	struct ifnet *ifp;
163133497Sandre	struct in_addr odest, dest;
164128872Sandre	u_short sum, ip_len;
165122702Sandre	int error = 0;
166133920Sandre	int hlen, mtu;
167133920Sandre#ifdef IPFIREWALL_FORWARD
168133920Sandre	struct m_tag *fwd_tag;
169133920Sandre#endif
170122702Sandre
171122702Sandre	/*
172122702Sandre	 * Are we active and forwarding packets?
173122702Sandre	 */
174122702Sandre	if (!ipfastforward_active || !ipforwarding)
175122702Sandre		return 0;
176122702Sandre
177122702Sandre	M_ASSERTVALID(m);
178122702Sandre	M_ASSERTPKTHDR(m);
179122702Sandre
180128872Sandre	ro.ro_rt = NULL;
181128872Sandre
182122702Sandre	/*
183122702Sandre	 * Step 1: check for packet drop conditions (and sanity checks)
184122702Sandre	 */
185122702Sandre
186122702Sandre	/*
187122702Sandre	 * Is entire packet big enough?
188122702Sandre	 */
189122702Sandre	if (m->m_pkthdr.len < sizeof(struct ip)) {
190122702Sandre		ipstat.ips_tooshort++;
191122702Sandre		goto drop;
192122702Sandre	}
193122702Sandre
194122702Sandre	/*
195122702Sandre	 * Is first mbuf large enough for ip header and is header present?
196122702Sandre	 */
197122702Sandre	if (m->m_len < sizeof (struct ip) &&
198122702Sandre	   (m = m_pullup(m, sizeof (struct ip))) == 0) {
199122702Sandre		ipstat.ips_toosmall++;
200122702Sandre		goto drop;
201122702Sandre	}
202122702Sandre
203122702Sandre	ip = mtod(m, struct ip *);
204122702Sandre
205122702Sandre	/*
206122702Sandre	 * Is it IPv4?
207122702Sandre	 */
208122702Sandre	if (ip->ip_v != IPVERSION) {
209122702Sandre		ipstat.ips_badvers++;
210122702Sandre		goto drop;
211122702Sandre	}
212122702Sandre
213122702Sandre	/*
214122702Sandre	 * Is IP header length correct and is it in first mbuf?
215122702Sandre	 */
216122702Sandre	hlen = ip->ip_hl << 2;
217122702Sandre	if (hlen < sizeof(struct ip)) {	/* minimum header length */
218122702Sandre		ipstat.ips_badlen++;
219122702Sandre		goto drop;
220122702Sandre	}
221122702Sandre	if (hlen > m->m_len) {
222122702Sandre		if ((m = m_pullup(m, hlen)) == 0) {
223122702Sandre			ipstat.ips_badhlen++;
224122702Sandre			goto drop;
225122702Sandre		}
226122702Sandre		ip = mtod(m, struct ip *);
227122702Sandre	}
228122702Sandre
229122702Sandre	/*
230122702Sandre	 * Checksum correct?
231122702Sandre	 */
232122702Sandre	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED)
233122702Sandre		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
234122702Sandre	else {
235122702Sandre		if (hlen == sizeof(struct ip))
236122702Sandre			sum = in_cksum_hdr(ip);
237122702Sandre		else
238122702Sandre			sum = in_cksum(m, hlen);
239122702Sandre	}
240122702Sandre	if (sum) {
241122702Sandre		ipstat.ips_badsum++;
242122702Sandre		goto drop;
243122702Sandre	}
244122702Sandre	m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
245122702Sandre
246128872Sandre	ip_len = ntohs(ip->ip_len);
247122702Sandre
248122702Sandre	/*
249122702Sandre	 * Is IP length longer than packet we have got?
250122702Sandre	 */
251128872Sandre	if (m->m_pkthdr.len < ip_len) {
252122702Sandre		ipstat.ips_tooshort++;
253122702Sandre		goto drop;
254122702Sandre	}
255122702Sandre
256122702Sandre	/*
257122702Sandre	 * Is packet longer than IP header tells us? If yes, truncate packet.
258122702Sandre	 */
259128872Sandre	if (m->m_pkthdr.len > ip_len) {
260122702Sandre		if (m->m_len == m->m_pkthdr.len) {
261128872Sandre			m->m_len = ip_len;
262128872Sandre			m->m_pkthdr.len = ip_len;
263122702Sandre		} else
264128872Sandre			m_adj(m, ip_len - m->m_pkthdr.len);
265122702Sandre	}
266122702Sandre
267122702Sandre	/*
268122702Sandre	 * Is packet from or to 127/8?
269122702Sandre	 */
270122702Sandre	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
271122702Sandre	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
272122702Sandre		ipstat.ips_badaddr++;
273122702Sandre		goto drop;
274122702Sandre	}
275122702Sandre
276133480Sandre#ifdef ALTQ
277122702Sandre	/*
278133480Sandre	 * Is packet dropped by traffic conditioner?
279133480Sandre	 */
280133480Sandre	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
281133480Sandre		return 1;
282133480Sandre#endif
283133480Sandre
284133480Sandre	/*
285122702Sandre	 * Step 2: fallback conditions to normal ip_input path processing
286122702Sandre	 */
287122702Sandre
288122702Sandre	/*
289122702Sandre	 * Only IP packets without options
290122702Sandre	 */
291129017Sandre	if (ip->ip_hl != (sizeof(struct ip) >> 2)) {
292129017Sandre		if (ip_doopts == 1)
293129017Sandre			return 0;
294129017Sandre		else if (ip_doopts == 2) {
295129017Sandre			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,
296129017Sandre				0, NULL);
297129017Sandre			return 1;
298129017Sandre		}
299129017Sandre		/* else ignore IP options and continue */
300129017Sandre	}
301122702Sandre
302122702Sandre	/*
303122702Sandre	 * Only unicast IP, not from loopback, no L2 or IP broadcast,
304122702Sandre	 * no multicast, no INADDR_ANY
305122702Sandre	 *
306122702Sandre	 * XXX: Probably some of these checks could be direct drop
307122702Sandre	 * conditions.  However it is not clear whether there are some
308122702Sandre	 * hacks or obscure behaviours which make it neccessary to
309122702Sandre	 * let ip_input handle it.  We play safe here and let ip_input
310122702Sandre	 * deal with it until it is proven that we can directly drop it.
311122702Sandre	 */
312122702Sandre	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
313122702Sandre	    ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
314122702Sandre	    ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
315122702Sandre	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
316122702Sandre	    IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
317122702Sandre	    ip->ip_dst.s_addr == INADDR_ANY )
318128872Sandre		return 0;
319122702Sandre
320122702Sandre	/*
321122702Sandre	 * Is it for a local address on this host?
322122702Sandre	 */
323133497Sandre	if (in_localip(ip->ip_dst))
324133497Sandre		return 0;
325122702Sandre
326122702Sandre	/*
327122702Sandre	 * Or is it for a local IP broadcast address on this host?
328122702Sandre	 */
329133482Sandre	if ((m->m_flags & M_BCAST) &&
330133482Sandre	    (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST)) {
331122702Sandre	        TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
332122702Sandre			if (ifa->ifa_addr->sa_family != AF_INET)
333122702Sandre				continue;
334122702Sandre			ia = ifatoia(ifa);
335122702Sandre			if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
336128872Sandre				return 0;
337122702Sandre			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
338122702Sandre			    ip->ip_dst.s_addr)
339128872Sandre				return 0;
340122702Sandre		}
341122702Sandre	}
342122702Sandre	ipstat.ips_total++;
343122702Sandre
344122702Sandre	/*
345122702Sandre	 * Step 3: incoming packet firewall processing
346122702Sandre	 */
347122702Sandre
348128872Sandre	/*
349128872Sandre	 * Convert to host representation
350128872Sandre	 */
351128872Sandre	ip->ip_len = ntohs(ip->ip_len);
352128872Sandre	ip->ip_off = ntohs(ip->ip_off);
353128872Sandre
354133497Sandre	odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;
355134383Sandre
356122702Sandre	/*
357122702Sandre	 * Run through list of ipfilter hooks for input packets
358122702Sandre	 */
359134383Sandre	if (inet_pfil_hook.ph_busy_count == -1)
360134383Sandre		goto passin;
361134383Sandre
362122702Sandre	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN) ||
363122702Sandre	    m == NULL)
364122702Sandre		return 1;
365122702Sandre
366122702Sandre	M_ASSERTVALID(m);
367122702Sandre	M_ASSERTPKTHDR(m);
368122702Sandre
369122702Sandre	ip = mtod(m, struct ip *);	/* m may have changed by pfil hook */
370133497Sandre	dest.s_addr = ip->ip_dst.s_addr;
371122702Sandre
372122702Sandre	/*
373122702Sandre	 * Destination address changed?
374122702Sandre	 */
375133497Sandre	if (odest.s_addr != dest.s_addr) {
376122702Sandre		/*
377122702Sandre		 * Is it now for a local address on this host?
378122702Sandre		 */
379133497Sandre		if (in_localip(dest))
380133497Sandre			goto forwardlocal;
381122702Sandre		/*
382122702Sandre		 * Go on with new destination address
383122702Sandre		 */
384122702Sandre	}
385133920Sandre#ifdef IPFIREWALL_FORWARD
386133920Sandre	if (m->m_flags & M_FASTFWD_OURS) {
387133920Sandre		/*
388133920Sandre		 * ipfw changed it for a local address on this host.
389133920Sandre		 */
390133920Sandre		goto forwardlocal;
391133920Sandre	}
392133920Sandre#endif /* IPFIREWALL_FORWARD */
393122702Sandre
394134383Sandrepassin:
395122702Sandre	/*
396122702Sandre	 * Step 4: decrement TTL and look up route
397122702Sandre	 */
398122702Sandre
399122702Sandre	/*
400122702Sandre	 * Check TTL
401122702Sandre	 */
402122702Sandre#ifdef IPSTEALTH
403122702Sandre	if (!ipstealth) {
404122702Sandre#endif
405122702Sandre	if (ip->ip_ttl <= IPTTLDEC) {
406123740Speter		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, NULL);
407122702Sandre		return 1;
408122702Sandre	}
409122702Sandre
410122702Sandre	/*
411122702Sandre	 * Decrement the TTL and incrementally change the checksum.
412122702Sandre	 * Don't bother doing this with hw checksum offloading.
413122702Sandre	 */
414122702Sandre	ip->ip_ttl -= IPTTLDEC;
415122702Sandre	if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
416122702Sandre		ip->ip_sum -= ~htons(IPTTLDEC << 8);
417122702Sandre	else
418122702Sandre		ip->ip_sum += htons(IPTTLDEC << 8);
419122702Sandre#ifdef IPSTEALTH
420122702Sandre	}
421122702Sandre#endif
422122702Sandre
423122702Sandre	/*
424122702Sandre	 * Find route to destination.
425122702Sandre	 */
426128872Sandre	if ((dst = ip_findroute(&ro, dest, m)) == NULL)
427128872Sandre		return 1;	/* icmp unreach already sent */
428128872Sandre	ifp = ro.ro_rt->rt_ifp;
429122702Sandre
430122702Sandre	/*
431122702Sandre	 * Step 5: outgoing firewall packet processing
432122702Sandre	 */
433122702Sandre
434122702Sandre	/*
435122702Sandre	 * Run through list of hooks for output packets.
436122702Sandre	 */
437134383Sandre	if (inet_pfil_hook.ph_busy_count == -1)
438134383Sandre		goto passout;
439134383Sandre
440122702Sandre	if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT) || m == NULL) {
441128872Sandre		goto consumed;
442122702Sandre	}
443122702Sandre
444122702Sandre	M_ASSERTVALID(m);
445122702Sandre	M_ASSERTPKTHDR(m);
446122702Sandre
447122702Sandre	ip = mtod(m, struct ip *);
448133497Sandre	dest.s_addr = ip->ip_dst.s_addr;
449122702Sandre
450122702Sandre	/*
451122702Sandre	 * Destination address changed?
452122702Sandre	 */
453133920Sandre#ifndef IPFIREWALL_FORWARD
454133497Sandre	if (odest.s_addr != dest.s_addr) {
455133920Sandre#else
456133920Sandre	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
457133920Sandre	if (odest.s_addr != dest.s_addr || fwd_tag != NULL) {
458133920Sandre#endif /* IPFIREWALL_FORWARD */
459122702Sandre		/*
460122702Sandre		 * Is it now for a local address on this host?
461122702Sandre		 */
462133920Sandre#ifndef IPFIREWALL_FORWARD
463133497Sandre		if (in_localip(dest)) {
464133920Sandre#else
465133920Sandre		if (in_localip(dest) || m->m_flags & M_FASTFWD_OURS) {
466133920Sandre#endif /* IPFIREWALL_FORWARD */
467122702Sandreforwardlocal:
468133497Sandre			/*
469135158Sandre			 * Return packet for processing by ip_input().
470135158Sandre			 * Keep host byte order as expected at ip_input's
471135158Sandre			 * "ours"-label.
472133497Sandre			 */
473135158Sandre			m->m_flags |= M_FASTFWD_OURS;
474133497Sandre			if (ro.ro_rt)
475133497Sandre				RTFREE(ro.ro_rt);
476133497Sandre			return 0;
477122702Sandre		}
478122702Sandre		/*
479122702Sandre		 * Redo route lookup with new destination address
480122702Sandre		 */
481133920Sandre#ifdef IPFIREWALL_FORWARD
482133920Sandre		if (fwd_tag) {
483133920Sandre			if (!in_localip(ip->ip_src) && !in_localaddr(ip->ip_dst))
484133920Sandre				dest.s_addr = ((struct sockaddr_in *)(fwd_tag+1))->sin_addr.s_addr;
485133920Sandre			m_tag_delete(m, fwd_tag);
486133920Sandre		}
487133920Sandre#endif /* IPFIREWALL_FORWARD */
488122702Sandre		RTFREE(ro.ro_rt);
489128872Sandre		if ((dst = ip_findroute(&ro, dest, m)) == NULL)
490128872Sandre			return 1;	/* icmp unreach already sent */
491128872Sandre		ifp = ro.ro_rt->rt_ifp;
492122702Sandre	}
493122702Sandre
494134383Sandrepassout:
495122702Sandre	/*
496122702Sandre	 * Step 6: send off the packet
497122702Sandre	 */
498122702Sandre
499122702Sandre	/*
500128872Sandre	 * Check if route is dampned (when ARP is unable to resolve)
501128872Sandre	 */
502128872Sandre	if ((ro.ro_rt->rt_flags & RTF_REJECT) &&
503128872Sandre	    ro.ro_rt->rt_rmx.rmx_expire >= time_second) {
504128872Sandre		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL);
505128872Sandre		goto consumed;
506128872Sandre	}
507128872Sandre
508133480Sandre#ifndef ALTQ
509128872Sandre	/*
510128872Sandre	 * Check if there is enough space in the interface queue
511128872Sandre	 */
512128872Sandre	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
513128872Sandre	    ifp->if_snd.ifq_maxlen) {
514128872Sandre		ipstat.ips_odropped++;
515128872Sandre		/* would send source quench here but that is depreciated */
516128872Sandre		goto drop;
517128872Sandre	}
518133480Sandre#endif
519128872Sandre
520128872Sandre	/*
521128872Sandre	 * Check if media link state of interface is not down
522128872Sandre	 */
523128872Sandre	if (ifp->if_link_state == LINK_STATE_DOWN) {
524128872Sandre		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL);
525128872Sandre		goto consumed;
526128872Sandre	}
527128872Sandre
528128872Sandre	/*
529122702Sandre	 * Check if packet fits MTU or if hardware will fragement for us
530122702Sandre	 */
531122702Sandre	if (ro.ro_rt->rt_rmx.rmx_mtu)
532122702Sandre		mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu);
533122702Sandre	else
534122702Sandre		mtu = ifp->if_mtu;
535122702Sandre
536122702Sandre	if (ip->ip_len <= mtu ||
537122702Sandre	    (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) {
538122702Sandre		/*
539122702Sandre		 * Restore packet header fields to original values
540122702Sandre		 */
541122702Sandre		ip->ip_len = htons(ip->ip_len);
542122702Sandre		ip->ip_off = htons(ip->ip_off);
543122702Sandre		/*
544122702Sandre		 * Send off the packet via outgoing interface
545122702Sandre		 */
546122702Sandre		error = (*ifp->if_output)(ifp, m,
547122702Sandre				(struct sockaddr *)dst, ro.ro_rt);
548122702Sandre	} else {
549122702Sandre		/*
550128872Sandre		 * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery
551122702Sandre		 */
552122702Sandre		if (ip->ip_off & IP_DF) {
553128872Sandre			ipstat.ips_cantfrag++;
554122702Sandre			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
555123740Speter				0, ifp);
556128872Sandre			goto consumed;
557122702Sandre		} else {
558122702Sandre			/*
559122702Sandre			 * We have to fragement the packet
560122702Sandre			 */
561122702Sandre			m->m_pkthdr.csum_flags |= CSUM_IP;
562128872Sandre			/*
563128872Sandre			 * ip_fragment expects ip_len and ip_off in host byte
564128872Sandre			 * order but returns all packets in network byte order
565128872Sandre			 */
566122702Sandre			if (ip_fragment(ip, &m, mtu, ifp->if_hwassist,
567122702Sandre					(~ifp->if_hwassist & CSUM_DELAY_IP))) {
568122702Sandre				goto drop;
569122702Sandre			}
570122702Sandre			KASSERT(m != NULL, ("null mbuf and no error"));
571122702Sandre			/*
572122702Sandre			 * Send off the fragments via outgoing interface
573122702Sandre			 */
574122702Sandre			error = 0;
575122702Sandre			do {
576122702Sandre				m0 = m->m_nextpkt;
577122702Sandre				m->m_nextpkt = NULL;
578122702Sandre
579122702Sandre				error = (*ifp->if_output)(ifp, m,
580122702Sandre					(struct sockaddr *)dst, ro.ro_rt);
581122702Sandre				if (error)
582122702Sandre					break;
583122702Sandre			} while ((m = m0) != NULL);
584122702Sandre			if (error) {
585122702Sandre				/* Reclaim remaining fragments */
586122702Sandre				for (; m; m = m0) {
587122702Sandre					m0 = m->m_nextpkt;
588122702Sandre					m->m_nextpkt = NULL;
589122702Sandre					m_freem(m);
590122702Sandre				}
591122702Sandre			} else
592122702Sandre				ipstat.ips_fragmented++;
593122702Sandre		}
594122702Sandre	}
595122702Sandre
596122702Sandre	if (error != 0)
597122702Sandre		ipstat.ips_odropped++;
598122702Sandre	else {
599122702Sandre		ro.ro_rt->rt_rmx.rmx_pksent++;
600122702Sandre		ipstat.ips_forward++;
601122702Sandre		ipstat.ips_fastforward++;
602122702Sandre	}
603128872Sandreconsumed:
604122702Sandre	RTFREE(ro.ro_rt);
605122702Sandre	return 1;
606122702Sandredrop:
607122702Sandre	if (m)
608122702Sandre		m_freem(m);
609128872Sandre	if (ro.ro_rt)
610128872Sandre		RTFREE(ro.ro_rt);
611122702Sandre	return 1;
612122702Sandre}
613