ip_fastfwd.c revision 166452
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 * $FreeBSD: head/sys/netinet/ip_fastfwd.c 166452 2007-02-03 06:46:48Z bms $
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
37148324Skeramida * 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 *
44148324Skeramida * We handle basic errors, IP header errors, checksum errors,
45122702Sandre * destination unreachable, fragmentation and fragmentation needed and
46148324Skeramida * 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 *
61148324Skeramida * 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
66148324Skeramida * ICMP redirects to connected hosts if the destination router was not the
67148324Skeramida * default gateway. In one case it was filling the routing table of a host
68148324Skeramida * with approximately 300.000 cloned redirect entries until it ran out of
69148324Skeramida * kernel memory. However the networking code proved very robust and it didn't
70148324Skeramida * crash or fail in other ways.
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>
103152592Sandre#include <netinet/ip_options.h>
104122702Sandre
105122702Sandre#include <machine/in_cksum.h>
106122702Sandre
107122702Sandrestatic int ipfastforward_active = 0;
108122702SandreSYSCTL_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW,
109122702Sandre    &ipfastforward_active, 0, "Enable fast IP forwarding");
110122702Sandre
111128872Sandrestatic struct sockaddr_in *
112133497Sandreip_findroute(struct route *ro, struct in_addr dest, struct mbuf *m)
113128872Sandre{
114128872Sandre	struct sockaddr_in *dst;
115128872Sandre	struct rtentry *rt;
116128872Sandre
117128872Sandre	/*
118128872Sandre	 * Find route to destination.
119128872Sandre	 */
120128872Sandre	bzero(ro, sizeof(*ro));
121128872Sandre	dst = (struct sockaddr_in *)&ro->ro_dst;
122128872Sandre	dst->sin_family = AF_INET;
123128872Sandre	dst->sin_len = sizeof(*dst);
124133497Sandre	dst->sin_addr.s_addr = dest.s_addr;
125128872Sandre	rtalloc_ign(ro, RTF_CLONING);
126128872Sandre
127128872Sandre	/*
128128872Sandre	 * Route there and interface still up?
129128872Sandre	 */
130128872Sandre	rt = ro->ro_rt;
131128872Sandre	if (rt && (rt->rt_flags & RTF_UP) &&
132128872Sandre	    (rt->rt_ifp->if_flags & IFF_UP) &&
133148887Srwatson	    (rt->rt_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
134128872Sandre		if (rt->rt_flags & RTF_GATEWAY)
135128872Sandre			dst = (struct sockaddr_in *)rt->rt_gateway;
136128872Sandre	} else {
137128872Sandre		ipstat.ips_noroute++;
138128872Sandre		ipstat.ips_cantforward++;
139128872Sandre		if (rt)
140128872Sandre			RTFREE(rt);
141145863Sandre		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
142128872Sandre		return NULL;
143128872Sandre	}
144128872Sandre	return dst;
145128872Sandre}
146128872Sandre
147122702Sandre/*
148122702Sandre * Try to forward a packet based on the destination address.
149122702Sandre * This is a fast path optimized for the plain forwarding case.
150122702Sandre * If the packet is handled (and consumed) here then we return 1;
151122702Sandre * otherwise 0 is returned and the packet should be delivered
152122702Sandre * to ip_input for full processing.
153122702Sandre */
154154518Sandrestruct mbuf *
155122702Sandreip_fastforward(struct mbuf *m)
156122702Sandre{
157122702Sandre	struct ip *ip;
158122702Sandre	struct mbuf *m0 = NULL;
159122702Sandre	struct route ro;
160122702Sandre	struct sockaddr_in *dst = NULL;
161128872Sandre	struct ifnet *ifp;
162133497Sandre	struct in_addr odest, dest;
163128872Sandre	u_short sum, ip_len;
164122702Sandre	int error = 0;
165133920Sandre	int hlen, mtu;
166133920Sandre#ifdef IPFIREWALL_FORWARD
167133920Sandre	struct m_tag *fwd_tag;
168133920Sandre#endif
169122702Sandre
170122702Sandre	/*
171122702Sandre	 * Are we active and forwarding packets?
172122702Sandre	 */
173122702Sandre	if (!ipfastforward_active || !ipforwarding)
174154518Sandre		return m;
175122702Sandre
176122702Sandre	M_ASSERTVALID(m);
177122702Sandre	M_ASSERTPKTHDR(m);
178122702Sandre
179128872Sandre	ro.ro_rt = NULL;
180128872Sandre
181122702Sandre	/*
182122702Sandre	 * Step 1: check for packet drop conditions (and sanity checks)
183122702Sandre	 */
184122702Sandre
185122702Sandre	/*
186122702Sandre	 * Is entire packet big enough?
187122702Sandre	 */
188122702Sandre	if (m->m_pkthdr.len < sizeof(struct ip)) {
189122702Sandre		ipstat.ips_tooshort++;
190122702Sandre		goto drop;
191122702Sandre	}
192122702Sandre
193122702Sandre	/*
194122702Sandre	 * Is first mbuf large enough for ip header and is header present?
195122702Sandre	 */
196122702Sandre	if (m->m_len < sizeof (struct ip) &&
197137302Sandre	   (m = m_pullup(m, sizeof (struct ip))) == NULL) {
198122702Sandre		ipstat.ips_toosmall++;
199154518Sandre		return NULL;	/* mbuf already free'd */
200122702Sandre	}
201122702Sandre
202122702Sandre	ip = mtod(m, struct ip *);
203122702Sandre
204122702Sandre	/*
205122702Sandre	 * Is it IPv4?
206122702Sandre	 */
207122702Sandre	if (ip->ip_v != IPVERSION) {
208122702Sandre		ipstat.ips_badvers++;
209122702Sandre		goto drop;
210122702Sandre	}
211122702Sandre
212122702Sandre	/*
213122702Sandre	 * Is IP header length correct and is it in first mbuf?
214122702Sandre	 */
215122702Sandre	hlen = ip->ip_hl << 2;
216122702Sandre	if (hlen < sizeof(struct ip)) {	/* minimum header length */
217122702Sandre		ipstat.ips_badlen++;
218122702Sandre		goto drop;
219122702Sandre	}
220122702Sandre	if (hlen > m->m_len) {
221154518Sandre		if ((m = m_pullup(m, hlen)) == NULL) {
222122702Sandre			ipstat.ips_badhlen++;
223154518Sandre			return NULL;	/* mbuf already free'd */
224122702Sandre		}
225122702Sandre		ip = mtod(m, struct ip *);
226122702Sandre	}
227122702Sandre
228122702Sandre	/*
229122702Sandre	 * Checksum correct?
230122702Sandre	 */
231122702Sandre	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED)
232122702Sandre		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
233122702Sandre	else {
234122702Sandre		if (hlen == sizeof(struct ip))
235122702Sandre			sum = in_cksum_hdr(ip);
236122702Sandre		else
237122702Sandre			sum = in_cksum(m, hlen);
238122702Sandre	}
239122702Sandre	if (sum) {
240122702Sandre		ipstat.ips_badsum++;
241122702Sandre		goto drop;
242122702Sandre	}
243136690Sandre
244136690Sandre	/*
245148324Skeramida	 * Remember that we have checked the IP header and found it valid.
246136690Sandre	 */
247122702Sandre	m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
248122702Sandre
249128872Sandre	ip_len = ntohs(ip->ip_len);
250122702Sandre
251122702Sandre	/*
252122702Sandre	 * Is IP length longer than packet we have got?
253122702Sandre	 */
254128872Sandre	if (m->m_pkthdr.len < ip_len) {
255122702Sandre		ipstat.ips_tooshort++;
256122702Sandre		goto drop;
257122702Sandre	}
258122702Sandre
259122702Sandre	/*
260122702Sandre	 * Is packet longer than IP header tells us? If yes, truncate packet.
261122702Sandre	 */
262128872Sandre	if (m->m_pkthdr.len > ip_len) {
263122702Sandre		if (m->m_len == m->m_pkthdr.len) {
264128872Sandre			m->m_len = ip_len;
265128872Sandre			m->m_pkthdr.len = ip_len;
266122702Sandre		} else
267128872Sandre			m_adj(m, ip_len - m->m_pkthdr.len);
268122702Sandre	}
269122702Sandre
270122702Sandre	/*
271122702Sandre	 * Is packet from or to 127/8?
272122702Sandre	 */
273122702Sandre	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
274122702Sandre	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
275122702Sandre		ipstat.ips_badaddr++;
276122702Sandre		goto drop;
277122702Sandre	}
278122702Sandre
279133480Sandre#ifdef ALTQ
280122702Sandre	/*
281133480Sandre	 * Is packet dropped by traffic conditioner?
282133480Sandre	 */
283133480Sandre	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
284154518Sandre		goto drop;
285133480Sandre#endif
286133480Sandre
287133480Sandre	/*
288122702Sandre	 * Step 2: fallback conditions to normal ip_input path processing
289122702Sandre	 */
290122702Sandre
291122702Sandre	/*
292122702Sandre	 * Only IP packets without options
293122702Sandre	 */
294129017Sandre	if (ip->ip_hl != (sizeof(struct ip) >> 2)) {
295129017Sandre		if (ip_doopts == 1)
296154518Sandre			return m;
297129017Sandre		else if (ip_doopts == 2) {
298129017Sandre			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,
299145863Sandre				0, 0);
300154518Sandre			return NULL;	/* mbuf already free'd */
301129017Sandre		}
302129017Sandre		/* else ignore IP options and continue */
303129017Sandre	}
304122702Sandre
305122702Sandre	/*
306122702Sandre	 * Only unicast IP, not from loopback, no L2 or IP broadcast,
307122702Sandre	 * no multicast, no INADDR_ANY
308122702Sandre	 *
309122702Sandre	 * XXX: Probably some of these checks could be direct drop
310122702Sandre	 * conditions.  However it is not clear whether there are some
311122702Sandre	 * hacks or obscure behaviours which make it neccessary to
312122702Sandre	 * let ip_input handle it.  We play safe here and let ip_input
313122702Sandre	 * deal with it until it is proven that we can directly drop it.
314122702Sandre	 */
315149369Sandre	if ((m->m_flags & (M_BCAST|M_MCAST)) ||
316149369Sandre	    (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
317122702Sandre	    ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
318122702Sandre	    ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
319122702Sandre	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
320122702Sandre	    IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
321166452Sbms	    IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) ||
322166452Sbms	    IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
323149369Sandre	    ip->ip_src.s_addr == INADDR_ANY ||
324122702Sandre	    ip->ip_dst.s_addr == INADDR_ANY )
325154518Sandre		return m;
326122702Sandre
327122702Sandre	/*
328122702Sandre	 * Is it for a local address on this host?
329122702Sandre	 */
330133497Sandre	if (in_localip(ip->ip_dst))
331154518Sandre		return m;
332122702Sandre
333122702Sandre	ipstat.ips_total++;
334122702Sandre
335122702Sandre	/*
336122702Sandre	 * Step 3: incoming packet firewall processing
337122702Sandre	 */
338122702Sandre
339128872Sandre	/*
340128872Sandre	 * Convert to host representation
341128872Sandre	 */
342128872Sandre	ip->ip_len = ntohs(ip->ip_len);
343128872Sandre	ip->ip_off = ntohs(ip->ip_off);
344128872Sandre
345133497Sandre	odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;
346134383Sandre
347122702Sandre	/*
348122702Sandre	 * Run through list of ipfilter hooks for input packets
349122702Sandre	 */
350155201Scsjp	if (!PFIL_HOOKED(&inet_pfil_hook))
351134383Sandre		goto passin;
352134383Sandre
353135920Smlaier	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL) ||
354122702Sandre	    m == NULL)
355154518Sandre		goto drop;
356122702Sandre
357122702Sandre	M_ASSERTVALID(m);
358122702Sandre	M_ASSERTPKTHDR(m);
359122702Sandre
360122702Sandre	ip = mtod(m, struct ip *);	/* m may have changed by pfil hook */
361133497Sandre	dest.s_addr = ip->ip_dst.s_addr;
362122702Sandre
363122702Sandre	/*
364122702Sandre	 * Destination address changed?
365122702Sandre	 */
366133497Sandre	if (odest.s_addr != dest.s_addr) {
367122702Sandre		/*
368122702Sandre		 * Is it now for a local address on this host?
369122702Sandre		 */
370133497Sandre		if (in_localip(dest))
371133497Sandre			goto forwardlocal;
372122702Sandre		/*
373122702Sandre		 * Go on with new destination address
374122702Sandre		 */
375122702Sandre	}
376133920Sandre#ifdef IPFIREWALL_FORWARD
377133920Sandre	if (m->m_flags & M_FASTFWD_OURS) {
378133920Sandre		/*
379133920Sandre		 * ipfw changed it for a local address on this host.
380133920Sandre		 */
381133920Sandre		goto forwardlocal;
382133920Sandre	}
383133920Sandre#endif /* IPFIREWALL_FORWARD */
384122702Sandre
385134383Sandrepassin:
386122702Sandre	/*
387122702Sandre	 * Step 4: decrement TTL and look up route
388122702Sandre	 */
389122702Sandre
390122702Sandre	/*
391122702Sandre	 * Check TTL
392122702Sandre	 */
393122702Sandre#ifdef IPSTEALTH
394122702Sandre	if (!ipstealth) {
395122702Sandre#endif
396122702Sandre	if (ip->ip_ttl <= IPTTLDEC) {
397145863Sandre		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
398154518Sandre		return NULL;	/* mbuf already free'd */
399122702Sandre	}
400122702Sandre
401122702Sandre	/*
402136690Sandre	 * Decrement the TTL and incrementally change the IP header checksum.
403136690Sandre	 * Don't bother doing this with hw checksum offloading, it's faster
404136690Sandre	 * doing it right here.
405122702Sandre	 */
406122702Sandre	ip->ip_ttl -= IPTTLDEC;
407122702Sandre	if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
408122702Sandre		ip->ip_sum -= ~htons(IPTTLDEC << 8);
409122702Sandre	else
410122702Sandre		ip->ip_sum += htons(IPTTLDEC << 8);
411122702Sandre#ifdef IPSTEALTH
412122702Sandre	}
413122702Sandre#endif
414122702Sandre
415122702Sandre	/*
416122702Sandre	 * Find route to destination.
417122702Sandre	 */
418128872Sandre	if ((dst = ip_findroute(&ro, dest, m)) == NULL)
419154518Sandre		return NULL;	/* icmp unreach already sent */
420128872Sandre	ifp = ro.ro_rt->rt_ifp;
421122702Sandre
422122702Sandre	/*
423137179Sbms	 * Immediately drop blackholed traffic.
424137179Sbms	 */
425137179Sbms	if (ro.ro_rt->rt_flags & RTF_BLACKHOLE)
426137179Sbms		goto drop;
427137179Sbms
428137179Sbms	/*
429122702Sandre	 * Step 5: outgoing firewall packet processing
430122702Sandre	 */
431122702Sandre
432122702Sandre	/*
433122702Sandre	 * Run through list of hooks for output packets.
434122702Sandre	 */
435155201Scsjp	if (!PFIL_HOOKED(&inet_pfil_hook))
436134383Sandre		goto passout;
437134383Sandre
438135920Smlaier	if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, NULL) || m == NULL) {
439154518Sandre		goto drop;
440122702Sandre	}
441122702Sandre
442122702Sandre	M_ASSERTVALID(m);
443122702Sandre	M_ASSERTPKTHDR(m);
444122702Sandre
445122702Sandre	ip = mtod(m, struct ip *);
446133497Sandre	dest.s_addr = ip->ip_dst.s_addr;
447122702Sandre
448122702Sandre	/*
449122702Sandre	 * Destination address changed?
450122702Sandre	 */
451133920Sandre#ifndef IPFIREWALL_FORWARD
452133497Sandre	if (odest.s_addr != dest.s_addr) {
453133920Sandre#else
454133920Sandre	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
455133920Sandre	if (odest.s_addr != dest.s_addr || fwd_tag != NULL) {
456133920Sandre#endif /* IPFIREWALL_FORWARD */
457122702Sandre		/*
458122702Sandre		 * Is it now for a local address on this host?
459122702Sandre		 */
460133920Sandre#ifndef IPFIREWALL_FORWARD
461133497Sandre		if (in_localip(dest)) {
462133920Sandre#else
463136690Sandre		if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) {
464133920Sandre#endif /* IPFIREWALL_FORWARD */
465122702Sandreforwardlocal:
466133497Sandre			/*
467135158Sandre			 * Return packet for processing by ip_input().
468135158Sandre			 * Keep host byte order as expected at ip_input's
469135158Sandre			 * "ours"-label.
470133497Sandre			 */
471135158Sandre			m->m_flags |= M_FASTFWD_OURS;
472133497Sandre			if (ro.ro_rt)
473133497Sandre				RTFREE(ro.ro_rt);
474154518Sandre			return m;
475122702Sandre		}
476122702Sandre		/*
477122702Sandre		 * Redo route lookup with new destination address
478122702Sandre		 */
479133920Sandre#ifdef IPFIREWALL_FORWARD
480133920Sandre		if (fwd_tag) {
481161380Sjulian			dest.s_addr = ((struct sockaddr_in *)
482157833Sglebius				    (fwd_tag + 1))->sin_addr.s_addr;
483133920Sandre			m_tag_delete(m, fwd_tag);
484133920Sandre		}
485133920Sandre#endif /* IPFIREWALL_FORWARD */
486122702Sandre		RTFREE(ro.ro_rt);
487128872Sandre		if ((dst = ip_findroute(&ro, dest, m)) == NULL)
488154518Sandre			return NULL;	/* icmp unreach already sent */
489128872Sandre		ifp = ro.ro_rt->rt_ifp;
490122702Sandre	}
491122702Sandre
492134383Sandrepassout:
493122702Sandre	/*
494122702Sandre	 * Step 6: send off the packet
495122702Sandre	 */
496122702Sandre
497122702Sandre	/*
498128872Sandre	 * Check if route is dampned (when ARP is unable to resolve)
499128872Sandre	 */
500128872Sandre	if ((ro.ro_rt->rt_flags & RTF_REJECT) &&
501150351Sandre	    ro.ro_rt->rt_rmx.rmx_expire >= time_uptime) {
502145863Sandre		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
503128872Sandre		goto consumed;
504128872Sandre	}
505128872Sandre
506133480Sandre#ifndef ALTQ
507128872Sandre	/*
508128872Sandre	 * Check if there is enough space in the interface queue
509128872Sandre	 */
510128872Sandre	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
511128872Sandre	    ifp->if_snd.ifq_maxlen) {
512128872Sandre		ipstat.ips_odropped++;
513128872Sandre		/* would send source quench here but that is depreciated */
514128872Sandre		goto drop;
515128872Sandre	}
516133480Sandre#endif
517128872Sandre
518128872Sandre	/*
519128872Sandre	 * Check if media link state of interface is not down
520128872Sandre	 */
521128872Sandre	if (ifp->if_link_state == LINK_STATE_DOWN) {
522145863Sandre		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
523128872Sandre		goto consumed;
524128872Sandre	}
525128872Sandre
526128872Sandre	/*
527148324Skeramida	 * Check if packet fits MTU or if hardware will fragment for us
528122702Sandre	 */
529122702Sandre	if (ro.ro_rt->rt_rmx.rmx_mtu)
530122702Sandre		mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu);
531122702Sandre	else
532122702Sandre		mtu = ifp->if_mtu;
533122702Sandre
534122702Sandre	if (ip->ip_len <= mtu ||
535122702Sandre	    (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) {
536122702Sandre		/*
537122702Sandre		 * Restore packet header fields to original values
538122702Sandre		 */
539122702Sandre		ip->ip_len = htons(ip->ip_len);
540122702Sandre		ip->ip_off = htons(ip->ip_off);
541122702Sandre		/*
542122702Sandre		 * Send off the packet via outgoing interface
543122702Sandre		 */
544122702Sandre		error = (*ifp->if_output)(ifp, m,
545122702Sandre				(struct sockaddr *)dst, ro.ro_rt);
546122702Sandre	} else {
547122702Sandre		/*
548128872Sandre		 * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery
549122702Sandre		 */
550122702Sandre		if (ip->ip_off & IP_DF) {
551128872Sandre			ipstat.ips_cantfrag++;
552122702Sandre			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
553145863Sandre				0, mtu);
554128872Sandre			goto consumed;
555122702Sandre		} else {
556122702Sandre			/*
557148324Skeramida			 * We have to fragment the packet
558122702Sandre			 */
559122702Sandre			m->m_pkthdr.csum_flags |= CSUM_IP;
560128872Sandre			/*
561128872Sandre			 * ip_fragment expects ip_len and ip_off in host byte
562128872Sandre			 * order but returns all packets in network byte order
563128872Sandre			 */
564122702Sandre			if (ip_fragment(ip, &m, mtu, ifp->if_hwassist,
565122702Sandre					(~ifp->if_hwassist & CSUM_DELAY_IP))) {
566122702Sandre				goto drop;
567122702Sandre			}
568122702Sandre			KASSERT(m != NULL, ("null mbuf and no error"));
569122702Sandre			/*
570122702Sandre			 * Send off the fragments via outgoing interface
571122702Sandre			 */
572122702Sandre			error = 0;
573122702Sandre			do {
574122702Sandre				m0 = m->m_nextpkt;
575122702Sandre				m->m_nextpkt = NULL;
576122702Sandre
577122702Sandre				error = (*ifp->if_output)(ifp, m,
578122702Sandre					(struct sockaddr *)dst, ro.ro_rt);
579122702Sandre				if (error)
580122702Sandre					break;
581122702Sandre			} while ((m = m0) != NULL);
582122702Sandre			if (error) {
583122702Sandre				/* Reclaim remaining fragments */
584144301Sglebius				for (m = m0; m; m = m0) {
585122702Sandre					m0 = m->m_nextpkt;
586122702Sandre					m_freem(m);
587122702Sandre				}
588122702Sandre			} else
589122702Sandre				ipstat.ips_fragmented++;
590122702Sandre		}
591122702Sandre	}
592122702Sandre
593122702Sandre	if (error != 0)
594122702Sandre		ipstat.ips_odropped++;
595122702Sandre	else {
596122702Sandre		ro.ro_rt->rt_rmx.rmx_pksent++;
597122702Sandre		ipstat.ips_forward++;
598122702Sandre		ipstat.ips_fastforward++;
599122702Sandre	}
600128872Sandreconsumed:
601122702Sandre	RTFREE(ro.ro_rt);
602154518Sandre	return NULL;
603122702Sandredrop:
604122702Sandre	if (m)
605122702Sandre		m_freem(m);
606128872Sandre	if (ro.ro_rt)
607128872Sandre		RTFREE(ro.ro_rt);
608154518Sandre	return NULL;
609122702Sandre}
610