ip_fastfwd.c revision 122759
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 122759 2003-11-15 17:03:37Z 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_ipdn.h"
80122702Sandre#include "opt_ipdivert.h"
81122702Sandre#include "opt_ipfilter.h"
82122702Sandre#include "opt_ipstealth.h"
83122702Sandre#include "opt_mac.h"
84122702Sandre#include "opt_pfil_hooks.h"
85122702Sandre
86122702Sandre#include <sys/param.h>
87122702Sandre#include <sys/systm.h>
88122702Sandre#include <sys/kernel.h>
89122702Sandre#include <sys/mac.h>
90122702Sandre#include <sys/malloc.h>
91122702Sandre#include <sys/mbuf.h>
92122702Sandre#include <sys/protosw.h>
93122702Sandre#include <sys/socket.h>
94122702Sandre#include <sys/sysctl.h>
95122702Sandre
96122702Sandre#include <net/pfil.h>
97122702Sandre#include <net/if.h>
98122702Sandre#include <net/if_types.h>
99122702Sandre#include <net/if_var.h>
100122702Sandre#include <net/if_dl.h>
101122702Sandre#include <net/route.h>
102122702Sandre
103122702Sandre#include <netinet/in.h>
104122702Sandre#include <netinet/in_systm.h>
105122702Sandre#include <netinet/in_var.h>
106122702Sandre#include <netinet/ip.h>
107122702Sandre#include <netinet/ip_var.h>
108122702Sandre#include <netinet/ip_icmp.h>
109122702Sandre
110122702Sandre#include <machine/in_cksum.h>
111122702Sandre
112122702Sandre#include <netinet/ip_fw.h>
113122702Sandre#include <netinet/ip_dummynet.h>
114122702Sandre
115122702Sandrestatic int ipfastforward_active = 0;
116122702SandreSYSCTL_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW,
117122702Sandre    &ipfastforward_active, 0, "Enable fast IP forwarding");
118122702Sandre
119122702Sandre/*
120122702Sandre * Try to forward a packet based on the destination address.
121122702Sandre * This is a fast path optimized for the plain forwarding case.
122122702Sandre * If the packet is handled (and consumed) here then we return 1;
123122702Sandre * otherwise 0 is returned and the packet should be delivered
124122702Sandre * to ip_input for full processing.
125122702Sandre */
126122702Sandreint
127122702Sandreip_fastforward(struct mbuf *m)
128122702Sandre{
129122702Sandre	struct ip *ip;
130122702Sandre	struct mbuf *m0 = NULL;
131122702Sandre#ifdef IPDIVERT
132122702Sandre	struct ip *tip;
133122702Sandre	struct mbuf *teem = NULL;
134122702Sandre#endif
135122702Sandre	struct mbuf *tag = NULL;
136122702Sandre	struct route ro;
137122702Sandre	struct sockaddr_in *dst = NULL;
138122702Sandre	struct in_ifaddr *ia = NULL;
139122702Sandre	struct ifaddr *ifa = NULL;
140122702Sandre	struct ifnet *ifp = NULL;
141122702Sandre	struct ip_fw_args args;
142122702Sandre	in_addr_t odest, dest;
143122702Sandre	u_short sum;
144122702Sandre	int error = 0;
145122702Sandre	int hlen, ipfw, mtu;
146122702Sandre
147122702Sandre	/*
148122702Sandre	 * Are we active and forwarding packets?
149122702Sandre	 */
150122702Sandre	if (!ipfastforward_active || !ipforwarding)
151122702Sandre		return 0;
152122702Sandre
153122702Sandre	/*
154122702Sandre	 * If there is any MT_TAG we fall back to ip_input because we can't
155122702Sandre	 * handle TAGs here. Should never happen as we get directly called
156122702Sandre	 * from the if_output routines.
157122702Sandre	 */
158122702Sandre	if (m->m_type == MT_TAG) {
159122702Sandre		KASSERT(0, ("%s: packet with MT_TAG not expected", __func__));
160122702Sandre		return 0;
161122702Sandre	}
162122702Sandre
163122702Sandre	M_ASSERTVALID(m);
164122702Sandre	M_ASSERTPKTHDR(m);
165122702Sandre
166122702Sandre	/*
167122702Sandre	 * Step 1: check for packet drop conditions (and sanity checks)
168122702Sandre	 */
169122702Sandre
170122702Sandre	/*
171122702Sandre	 * Is entire packet big enough?
172122702Sandre	 */
173122702Sandre	if (m->m_pkthdr.len < sizeof(struct ip)) {
174122702Sandre		ipstat.ips_tooshort++;
175122702Sandre		goto drop;
176122702Sandre	}
177122702Sandre
178122702Sandre	/*
179122702Sandre	 * Is first mbuf large enough for ip header and is header present?
180122702Sandre	 */
181122702Sandre	if (m->m_len < sizeof (struct ip) &&
182122702Sandre	   (m = m_pullup(m, sizeof (struct ip))) == 0) {
183122702Sandre		ipstat.ips_toosmall++;
184122702Sandre		goto drop;
185122702Sandre	}
186122702Sandre
187122702Sandre	ip = mtod(m, struct ip *);
188122702Sandre
189122702Sandre	/*
190122702Sandre	 * Is it IPv4?
191122702Sandre	 */
192122702Sandre	if (ip->ip_v != IPVERSION) {
193122702Sandre		ipstat.ips_badvers++;
194122702Sandre		goto drop;
195122702Sandre	}
196122702Sandre
197122702Sandre	/*
198122702Sandre	 * Is IP header length correct and is it in first mbuf?
199122702Sandre	 */
200122702Sandre	hlen = ip->ip_hl << 2;
201122702Sandre	if (hlen < sizeof(struct ip)) {	/* minimum header length */
202122702Sandre		ipstat.ips_badlen++;
203122702Sandre		goto drop;
204122702Sandre	}
205122702Sandre	if (hlen > m->m_len) {
206122702Sandre		if ((m = m_pullup(m, hlen)) == 0) {
207122702Sandre			ipstat.ips_badhlen++;
208122702Sandre			goto drop;
209122702Sandre		}
210122702Sandre		ip = mtod(m, struct ip *);
211122702Sandre	}
212122702Sandre
213122702Sandre	/*
214122702Sandre	 * Checksum correct?
215122702Sandre	 */
216122702Sandre	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED)
217122702Sandre		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
218122702Sandre	else {
219122702Sandre		if (hlen == sizeof(struct ip))
220122702Sandre			sum = in_cksum_hdr(ip);
221122702Sandre		else
222122702Sandre			sum = in_cksum(m, hlen);
223122702Sandre	}
224122702Sandre	if (sum) {
225122702Sandre		ipstat.ips_badsum++;
226122702Sandre		goto drop;
227122702Sandre	}
228122702Sandre	m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
229122702Sandre
230122702Sandre	/*
231122702Sandre	 * Convert to host representation
232122702Sandre	 */
233122702Sandre	ip->ip_len = ntohs(ip->ip_len);
234122702Sandre	ip->ip_off = ntohs(ip->ip_off);
235122702Sandre
236122702Sandre	/*
237122702Sandre	 * Is IP length longer than packet we have got?
238122702Sandre	 */
239122702Sandre	if (m->m_pkthdr.len < ip->ip_len) {
240122702Sandre		ipstat.ips_tooshort++;
241122702Sandre		goto drop;
242122702Sandre	}
243122702Sandre
244122702Sandre	/*
245122702Sandre	 * Is packet longer than IP header tells us? If yes, truncate packet.
246122702Sandre	 */
247122702Sandre	if (m->m_pkthdr.len > ip->ip_len) {
248122702Sandre		if (m->m_len == m->m_pkthdr.len) {
249122702Sandre			m->m_len = ip->ip_len;
250122702Sandre			m->m_pkthdr.len = ip->ip_len;
251122702Sandre		} else
252122702Sandre			m_adj(m, ip->ip_len - m->m_pkthdr.len);
253122702Sandre	}
254122702Sandre
255122702Sandre	/*
256122702Sandre	 * Is packet from or to 127/8?
257122702Sandre	 */
258122702Sandre	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
259122702Sandre	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
260122702Sandre		ipstat.ips_badaddr++;
261122702Sandre		goto drop;
262122702Sandre	}
263122702Sandre
264122702Sandre	/*
265122702Sandre	 * Step 2: fallback conditions to normal ip_input path processing
266122702Sandre	 */
267122702Sandre
268122702Sandre	/*
269122702Sandre	 * Only IP packets without options
270122702Sandre	 */
271122702Sandre	if (ip->ip_hl != (sizeof(struct ip) >> 2))
272122702Sandre		goto fallback;
273122702Sandre
274122702Sandre	/*
275122702Sandre	 * Only unicast IP, not from loopback, no L2 or IP broadcast,
276122702Sandre	 * no multicast, no INADDR_ANY
277122702Sandre	 *
278122702Sandre	 * XXX: Probably some of these checks could be direct drop
279122702Sandre	 * conditions.  However it is not clear whether there are some
280122702Sandre	 * hacks or obscure behaviours which make it neccessary to
281122702Sandre	 * let ip_input handle it.  We play safe here and let ip_input
282122702Sandre	 * deal with it until it is proven that we can directly drop it.
283122702Sandre	 */
284122702Sandre	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
285122702Sandre	    ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
286122702Sandre	    ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
287122702Sandre	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
288122702Sandre	    IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
289122702Sandre	    ip->ip_dst.s_addr == INADDR_ANY )
290122702Sandre		goto fallback;
291122702Sandre
292122702Sandre	/*
293122702Sandre	 * Is it for a local address on this host?
294122702Sandre	 */
295122702Sandre	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
296122702Sandre		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
297122702Sandre			goto fallback;
298122702Sandre	}
299122702Sandre
300122702Sandre	/*
301122702Sandre	 * Or is it for a local IP broadcast address on this host?
302122702Sandre	 */
303122702Sandre	if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
304122702Sandre	        TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
305122702Sandre			if (ifa->ifa_addr->sa_family != AF_INET)
306122702Sandre				continue;
307122702Sandre			ia = ifatoia(ifa);
308122702Sandre			if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
309122702Sandre				goto fallback;
310122702Sandre			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
311122702Sandre			    ip->ip_dst.s_addr)
312122702Sandre				goto fallback;
313122702Sandre			continue;
314122702Sandrefallback:
315122702Sandre			/* return packet back to netisr for slow processing */
316122702Sandre			ip->ip_len = htons(ip->ip_len);
317122702Sandre			ip->ip_off = htons(ip->ip_off);
318122702Sandre			return 0;
319122702Sandre		}
320122702Sandre	}
321122702Sandre	ipstat.ips_total++;
322122702Sandre
323122702Sandre	/*
324122702Sandre	 * Step 3: incoming packet firewall processing
325122702Sandre	 */
326122702Sandre
327122702Sandre	odest = dest = ip->ip_dst.s_addr;
328122702Sandre#ifdef PFIL_HOOKS
329122702Sandre	/*
330122702Sandre	 * Run through list of ipfilter hooks for input packets
331122702Sandre	 */
332122702Sandre	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN) ||
333122702Sandre	    m == NULL)
334122702Sandre		return 1;
335122702Sandre
336122702Sandre	M_ASSERTVALID(m);
337122702Sandre	M_ASSERTPKTHDR(m);
338122702Sandre
339122702Sandre	ip = mtod(m, struct ip *);	/* m may have changed by pfil hook */
340122702Sandre	dest = ip->ip_dst.s_addr;
341122702Sandre#endif
342122702Sandre
343122702Sandre	/*
344122702Sandre	 * Run through ipfw for input packets
345122702Sandre	 */
346122702Sandre	if (fw_enable && IPFW_LOADED) {
347122702Sandre		bzero(&args, sizeof(args));
348122702Sandre		args.m = m;
349122702Sandre
350122702Sandre		ipfw = ip_fw_chk_ptr(&args);
351122702Sandre		m = args.m;
352122702Sandre
353122702Sandre		M_ASSERTVALID(m);
354122702Sandre		M_ASSERTPKTHDR(m);
355122702Sandre
356122702Sandre		/*
357122702Sandre		 * Packet denied, drop it
358122702Sandre		 */
359122702Sandre		if ((ipfw & IP_FW_PORT_DENY_FLAG) || m == NULL)
360122702Sandre			goto drop;
361122702Sandre		/*
362122702Sandre		 * Send packet to the appropriate pipe
363122702Sandre		 */
364122702Sandre		if (DUMMYNET_LOADED && (ipfw & IP_FW_PORT_DYNT_FLAG) != 0) {
365122702Sandre			ip_dn_io_ptr(m, ipfw & 0xffff, DN_TO_IP_IN, &args);
366122702Sandre			return 1;
367122702Sandre		}
368122702Sandre#ifdef IPDIVERT
369122702Sandre		/*
370122702Sandre		 * Divert packet
371122702Sandre		 */
372122702Sandre		if (ipfw != 0 && (ipfw & IP_FW_PORT_DYNT_FLAG) == 0) {
373122702Sandre			/*
374122702Sandre			 * See if this is a fragment
375122702Sandre			 */
376122702Sandre			if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
377122702Sandre				MGETHDR(tag, M_DONTWAIT, MT_TAG);
378122702Sandre				if (tag == NULL)
379122702Sandre					goto drop;
380122702Sandre				tag->m_flags = PACKET_TAG_DIVERT;
381122702Sandre				tag->m_data = (caddr_t)(u_long)args.divert_rule;
382122702Sandre				tag->m_next = m;
383122702Sandre				/* XXX: really bloody hack, see ip_input */
384122702Sandre				tag->m_nextpkt = (struct mbuf *)1;
385122702Sandre				m = tag;
386122702Sandre				tag = NULL;
387122702Sandre
388122702Sandre				goto droptoours;
389122702Sandre			}
390122702Sandre			/*
391122702Sandre			 * Tee packet
392122702Sandre			 */
393122702Sandre			if ((ipfw & IP_FW_PORT_TEE_FLAG) != 0)
394122702Sandre				teem = m_dup(m, M_DONTWAIT);
395122702Sandre			else
396122702Sandre				teem = m;
397122702Sandre			if (teem == NULL)
398122702Sandre				goto passin;
399122702Sandre
400122702Sandre			/*
401122702Sandre			 * Delayed checksums are not compatible
402122702Sandre			 */
403122702Sandre			if (teem->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
404122702Sandre				in_delayed_cksum(teem);
405122702Sandre				teem->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
406122702Sandre			}
407122702Sandre			/*
408122702Sandre			 * Restore packet header fields to original values
409122702Sandre			 */
410122702Sandre			tip = mtod(teem, struct ip *);
411122702Sandre			tip->ip_len = htons(tip->ip_len);
412122702Sandre			tip->ip_off = htons(tip->ip_off);
413122702Sandre			/*
414122702Sandre			 * Deliver packet to divert input routine
415122702Sandre			 */
416122702Sandre			divert_packet(teem, 0, ipfw & 0xffff, args.divert_rule);
417122702Sandre			/*
418122702Sandre			 * If this was not tee, we are done
419122702Sandre			 */
420122702Sandre			if ((ipfw & IP_FW_PORT_TEE_FLAG) == 0)
421122702Sandre				return 1;
422122702Sandre			/* Continue if it was tee */
423122702Sandre			goto passin;
424122702Sandre		}
425122702Sandre#endif
426122702Sandre		if (ipfw == 0 && args.next_hop != NULL) {
427122702Sandre			dest = args.next_hop->sin_addr.s_addr;
428122702Sandre			goto passin;
429122702Sandre		}
430122702Sandre		/*
431122702Sandre		 * Let through or not?
432122702Sandre		 */
433122702Sandre		if (ipfw != 0)
434122702Sandre			goto drop;
435122702Sandre	}
436122702Sandrepassin:
437122702Sandre	ip = mtod(m, struct ip *);	/* if m changed during fw processing */
438122702Sandre
439122702Sandre	/*
440122702Sandre	 * Destination address changed?
441122702Sandre	 */
442122702Sandre	if (odest != dest) {
443122702Sandre		/*
444122702Sandre		 * Is it now for a local address on this host?
445122702Sandre		 */
446122702Sandre		LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
447122702Sandre			if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
448122702Sandre				goto forwardlocal;
449122702Sandre		}
450122702Sandre		/*
451122702Sandre		 * Go on with new destination address
452122702Sandre		 */
453122702Sandre	}
454122702Sandre
455122702Sandre	/*
456122702Sandre	 * Step 4: decrement TTL and look up route
457122702Sandre	 */
458122702Sandre
459122702Sandre	/*
460122702Sandre	 * Check TTL
461122702Sandre	 */
462122702Sandre#ifdef IPSTEALTH
463122702Sandre	if (!ipstealth) {
464122702Sandre#endif
465122702Sandre	if (ip->ip_ttl <= IPTTLDEC) {
466122702Sandre		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, NULL, NULL);
467122702Sandre		return 1;
468122702Sandre	}
469122702Sandre
470122702Sandre	/*
471122702Sandre	 * Decrement the TTL and incrementally change the checksum.
472122702Sandre	 * Don't bother doing this with hw checksum offloading.
473122702Sandre	 */
474122702Sandre	ip->ip_ttl -= IPTTLDEC;
475122702Sandre	if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
476122702Sandre		ip->ip_sum -= ~htons(IPTTLDEC << 8);
477122702Sandre	else
478122702Sandre		ip->ip_sum += htons(IPTTLDEC << 8);
479122702Sandre#ifdef IPSTEALTH
480122702Sandre	}
481122702Sandre#endif
482122702Sandre
483122702Sandre	/*
484122702Sandre	 * Find route to destination.
485122702Sandre	 */
486122702Sandre	bzero(&ro, sizeof(ro));
487122702Sandre	dst = (struct sockaddr_in *)&ro.ro_dst;
488122702Sandre	dst->sin_family = AF_INET;
489122702Sandre	dst->sin_len = sizeof(*dst);
490122702Sandre	dst->sin_addr.s_addr = dest;
491122702Sandre	rtalloc_ign(&ro, (RTF_PRCLONING | RTF_CLONING));
492122702Sandre
493122702Sandre	/*
494122702Sandre	 * Route there and interface still up?
495122702Sandre	 */
496122702Sandre	if (ro.ro_rt &&
497122702Sandre	    (ro.ro_rt->rt_flags & RTF_UP) &&
498122702Sandre	    (ro.ro_rt->rt_ifp->if_flags & IFF_UP)) {
499122702Sandre		ia = ifatoia(ro.ro_rt->rt_ifa);
500122702Sandre		ifp = ro.ro_rt->rt_ifp;
501122702Sandre		if (ro.ro_rt->rt_flags & RTF_GATEWAY)
502122702Sandre			dst = (struct sockaddr_in *)ro.ro_rt->rt_gateway;
503122702Sandre	} else {
504122702Sandre		ipstat.ips_noroute++;
505122702Sandre		ipstat.ips_cantforward++;
506122702Sandre		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, NULL, NULL);
507122702Sandre		if (ro.ro_rt)
508122702Sandre			RTFREE(ro.ro_rt);
509122702Sandre		return 1;
510122702Sandre	}
511122702Sandre
512122702Sandre	/*
513122702Sandre	 * Step 5: outgoing firewall packet processing
514122702Sandre	 */
515122702Sandre
516122702Sandre#ifdef PFIL_HOOKS
517122702Sandre	/*
518122702Sandre	 * Run through list of hooks for output packets.
519122702Sandre	 */
520122702Sandre	if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT) || m == NULL) {
521122702Sandre		RTFREE(ro.ro_rt);
522122702Sandre		return 1;
523122702Sandre	}
524122702Sandre
525122702Sandre	M_ASSERTVALID(m);
526122702Sandre	M_ASSERTPKTHDR(m);
527122702Sandre
528122702Sandre	ip = mtod(m, struct ip *);
529122702Sandre	dest = ip->ip_dst.s_addr;
530122702Sandre#endif
531122702Sandre	if (fw_enable && IPFW_LOADED && !args.next_hop) {
532122759Sandre		bzero(&args, sizeof(args));
533122702Sandre		args.m = m;
534122702Sandre		args.oif = ifp;
535122702Sandre
536122702Sandre		ipfw = ip_fw_chk_ptr(&args);
537122702Sandre		m = args.m;
538122702Sandre
539122702Sandre		M_ASSERTVALID(m);
540122702Sandre		M_ASSERTPKTHDR(m);
541122702Sandre
542122702Sandre		if ((ipfw & IP_FW_PORT_DENY_FLAG) || m == NULL) {
543122702Sandre			RTFREE(ro.ro_rt);
544122702Sandre			goto drop;
545122702Sandre		}
546122702Sandre		if (DUMMYNET_LOADED && (ipfw & IP_FW_PORT_DYNT_FLAG) != 0) {
547122702Sandre			/*
548122702Sandre			 * XXX note: if the ifp or rt entry are deleted
549122702Sandre			 * while a pkt is in dummynet, we are in trouble!
550122702Sandre			 */
551122702Sandre			args.ro = &ro;		/* dummynet does not save it */
552122702Sandre			args.dst = dst;
553122702Sandre
554122702Sandre			ip_dn_io_ptr(m, ipfw & 0xffff, DN_TO_IP_OUT, &args);
555122702Sandre			RTFREE(ro.ro_rt);
556122702Sandre			return 1;
557122702Sandre		}
558122702Sandre#ifdef IPDIVERT
559122702Sandre		if (ipfw != 0 && (ipfw & IP_FW_PORT_DYNT_FLAG) == 0) {
560122702Sandre			/*
561122702Sandre			 * See if this is a fragment
562122702Sandre			 */
563122702Sandre			if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
564122702Sandre				MGETHDR(tag, M_DONTWAIT, MT_TAG);
565122702Sandre				if (tag == NULL) {
566122702Sandre					RTFREE(ro.ro_rt);
567122702Sandre					goto drop;
568122702Sandre				}
569122702Sandre				tag->m_flags = PACKET_TAG_DIVERT;
570122759Sandre				tag->m_data = (caddr_t)(u_long)args.divert_rule;
571122702Sandre				tag->m_next = m;
572122702Sandre				/* XXX: really bloody hack, see ip_input */
573122702Sandre				tag->m_nextpkt = (struct mbuf *)1;
574122702Sandre				m = tag;
575122702Sandre				tag = NULL;
576122702Sandre
577122702Sandre				goto droptoours;
578122702Sandre			}
579122702Sandre			/*
580122702Sandre			 * Tee packet
581122702Sandre			 */
582122702Sandre			if ((ipfw & IP_FW_PORT_TEE_FLAG) != 0)
583122702Sandre				teem = m_dup(m, M_DONTWAIT);
584122702Sandre			else
585122702Sandre				teem = m;
586122702Sandre			if (teem == NULL)
587122702Sandre				goto passout;
588122702Sandre
589122702Sandre			/*
590122702Sandre			 * Delayed checksums are not compatible with divert
591122702Sandre			 */
592122702Sandre			if (teem->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
593122702Sandre				in_delayed_cksum(teem);
594122702Sandre				teem->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
595122702Sandre			}
596122702Sandre			/*
597122702Sandre			 * Restore packet header fields to original values
598122702Sandre			 */
599122702Sandre			tip = mtod(teem, struct ip *);
600122702Sandre			tip->ip_len = htons(tip->ip_len);
601122702Sandre			tip->ip_off = htons(tip->ip_off);
602122702Sandre			/*
603122702Sandre			 * Deliver packet to divert input routine
604122702Sandre			 */
605122702Sandre			divert_packet(teem, 0, ipfw & 0xffff, args.divert_rule);
606122702Sandre			/*
607122702Sandre			 * If this was not tee, we are done
608122702Sandre			 */
609122702Sandre			if ((ipfw & IP_FW_PORT_TEE_FLAG) == 0) {
610122702Sandre				RTFREE(ro.ro_rt);
611122702Sandre				return 1;
612122702Sandre			}
613122702Sandre			/* Continue if it was tee */
614122702Sandre			goto passout;
615122702Sandre		}
616122702Sandre#endif
617122702Sandre		if (ipfw == 0 && args.next_hop != NULL) {
618122702Sandre			dest = args.next_hop->sin_addr.s_addr;
619122702Sandre			goto passout;
620122702Sandre		}
621122702Sandre		/*
622122702Sandre		 * Let through or not?
623122702Sandre		 */
624122702Sandre		if (ipfw != 0)
625122702Sandre			goto drop;
626122702Sandre	}
627122702Sandrepassout:
628122702Sandre	ip = mtod(m, struct ip *);
629122702Sandre
630122702Sandre	/*
631122702Sandre	 * Destination address changed?
632122702Sandre	 */
633122702Sandre	if (odest != dest) {
634122702Sandre		/*
635122702Sandre		 * Is it now for a local address on this host?
636122702Sandre		 */
637122702Sandre		LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
638122702Sandre			if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) {
639122702Sandreforwardlocal:
640122702Sandre				if (args.next_hop) {
641122702Sandre					/* XXX leak */
642122702Sandre					MGETHDR(tag, M_DONTWAIT, MT_TAG);
643122702Sandre					if (tag == NULL) {
644122702Sandre						if (ro.ro_rt)
645122702Sandre							RTFREE(ro.ro_rt);
646122702Sandre						goto drop;
647122702Sandre					}
648122702Sandre					tag->m_flags = PACKET_TAG_IPFORWARD;
649122702Sandre					tag->m_data = (caddr_t)args.next_hop;
650122702Sandre					tag->m_next = m;
651122702Sandre					/* XXX: really bloody hack,
652122702Sandre					 * see ip_input */
653122702Sandre					tag->m_nextpkt = (struct mbuf *)1;
654122702Sandre					m = tag;
655122702Sandre					tag = NULL;
656122702Sandre				}
657122702Sandre#ifdef IPDIVERT
658122702Sandredroptoours:	/* Used for DIVERT */
659122702Sandre#endif
660122702Sandre				MGETHDR(tag, M_DONTWAIT, MT_TAG);
661122702Sandre				if (tag == NULL) {
662122702Sandre					if (ro.ro_rt)
663122702Sandre						RTFREE(ro.ro_rt);
664122702Sandre					goto drop;
665122702Sandre				}
666122702Sandre				tag->m_flags = PACKET_TAG_IPFASTFWD_OURS;
667122702Sandre				tag->m_data = NULL;
668122702Sandre				tag->m_next = m;
669122702Sandre				/* XXX: really bloody hack, see ip_input */
670122702Sandre				tag->m_nextpkt = (struct mbuf *)1;
671122702Sandre				m = tag;
672122702Sandre				tag = NULL;
673122702Sandre
674122702Sandre				/* ip still points to the real packet */
675122702Sandre				ip->ip_len = htons(ip->ip_len);
676122702Sandre				ip->ip_off = htons(ip->ip_off);
677122702Sandre
678122702Sandre				/*
679122702Sandre				 * Return packet for processing by ip_input
680122702Sandre				 */
681122702Sandre				if (ro.ro_rt)
682122702Sandre					RTFREE(ro.ro_rt);
683122702Sandre				return 0;
684122702Sandre			}
685122702Sandre		}
686122702Sandre		/*
687122702Sandre		 * Redo route lookup with new destination address
688122702Sandre		 */
689122702Sandre		RTFREE(ro.ro_rt);
690122702Sandre		bzero(&ro, sizeof(ro));
691122702Sandre		dst = (struct sockaddr_in *)&ro.ro_dst;
692122702Sandre		dst->sin_family = AF_INET;
693122702Sandre		dst->sin_len = sizeof(*dst);
694122702Sandre		dst->sin_addr.s_addr = dest;
695122702Sandre		rtalloc_ign(&ro, (RTF_PRCLONING | RTF_CLONING));
696122702Sandre
697122702Sandre		/*
698122702Sandre		 * Route there and interface still up?
699122702Sandre		 */
700122702Sandre		if (ro.ro_rt &&
701122702Sandre		    (ro.ro_rt->rt_flags & RTF_UP) &&
702122702Sandre		    (ro.ro_rt->rt_ifp->if_flags & IFF_UP)) {
703122702Sandre			ia = ifatoia(ro.ro_rt->rt_ifa);
704122702Sandre			ifp = ro.ro_rt->rt_ifp;
705122702Sandre			if (ro.ro_rt->rt_flags & RTF_GATEWAY)
706122702Sandre				dst = (struct sockaddr_in *)ro.ro_rt->rt_gateway;
707122702Sandre		} else {
708122702Sandre			ipstat.ips_noroute++;
709122702Sandre			ipstat.ips_cantforward++;
710122702Sandre			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST,
711122702Sandre				NULL, NULL);
712122702Sandre			if (ro.ro_rt)
713122702Sandre				RTFREE(ro.ro_rt);
714122702Sandre			return 1;
715122702Sandre		}
716122702Sandre	}
717122702Sandre
718122702Sandre	/*
719122702Sandre	 * Step 6: send off the packet
720122702Sandre	 */
721122702Sandre
722122702Sandre	/*
723122702Sandre	 * Check if packet fits MTU or if hardware will fragement for us
724122702Sandre	 */
725122702Sandre	if (ro.ro_rt->rt_rmx.rmx_mtu)
726122702Sandre		mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu);
727122702Sandre	else
728122702Sandre		mtu = ifp->if_mtu;
729122702Sandre
730122702Sandre	if (ip->ip_len <= mtu ||
731122702Sandre	    (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) {
732122702Sandre		/*
733122702Sandre		 * Restore packet header fields to original values
734122702Sandre		 */
735122702Sandre		ip->ip_len = htons(ip->ip_len);
736122702Sandre		ip->ip_off = htons(ip->ip_off);
737122702Sandre		/*
738122702Sandre		 * Send off the packet via outgoing interface
739122702Sandre		 */
740122702Sandre		error = (*ifp->if_output)(ifp, m,
741122702Sandre				(struct sockaddr *)dst, ro.ro_rt);
742122702Sandre		if (ia) {
743122702Sandre			ia->ia_ifa.if_opackets++;
744122702Sandre			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
745122702Sandre		}
746122702Sandre	} else {
747122702Sandre		/*
748122702Sandre		 * Handle EMSGSIZE with icmp reply
749122702Sandre		 * needfrag for TCP MTU discovery
750122702Sandre		 */
751122702Sandre		if (ip->ip_off & IP_DF) {
752122702Sandre			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
753122702Sandre				NULL, ifp);
754122702Sandre			ipstat.ips_cantfrag++;
755122702Sandre			RTFREE(ro.ro_rt);
756122702Sandre			return 1;
757122702Sandre		} else {
758122702Sandre			/*
759122702Sandre			 * We have to fragement the packet
760122702Sandre			 */
761122702Sandre			m->m_pkthdr.csum_flags |= CSUM_IP;
762122702Sandre			if (ip_fragment(ip, &m, mtu, ifp->if_hwassist,
763122702Sandre					(~ifp->if_hwassist & CSUM_DELAY_IP))) {
764122702Sandre				RTFREE(ro.ro_rt);
765122702Sandre				goto drop;
766122702Sandre			}
767122702Sandre			KASSERT(m != NULL, ("null mbuf and no error"));
768122702Sandre			/*
769122702Sandre			 * Send off the fragments via outgoing interface
770122702Sandre			 */
771122702Sandre			error = 0;
772122702Sandre			do {
773122702Sandre				m0 = m->m_nextpkt;
774122702Sandre				m->m_nextpkt = NULL;
775122702Sandre
776122702Sandre				error = (*ifp->if_output)(ifp, m,
777122702Sandre					(struct sockaddr *)dst, ro.ro_rt);
778122702Sandre				if (error)
779122702Sandre					break;
780122702Sandre			} while ((m = m0) != NULL);
781122702Sandre			if (error) {
782122702Sandre				/* Reclaim remaining fragments */
783122702Sandre				for (; m; m = m0) {
784122702Sandre					m0 = m->m_nextpkt;
785122702Sandre					m->m_nextpkt = NULL;
786122702Sandre					m_freem(m);
787122702Sandre				}
788122702Sandre			} else
789122702Sandre				ipstat.ips_fragmented++;
790122702Sandre		}
791122702Sandre	}
792122702Sandre
793122702Sandre	if (error != 0)
794122702Sandre		ipstat.ips_odropped++;
795122702Sandre	else {
796122702Sandre		ro.ro_rt->rt_rmx.rmx_pksent++;
797122702Sandre		ipstat.ips_forward++;
798122702Sandre		ipstat.ips_fastforward++;
799122702Sandre	}
800122702Sandre	RTFREE(ro.ro_rt);
801122702Sandre	return 1;
802122702Sandredrop:
803122702Sandre	if (m)
804122702Sandre		m_freem(m);
805122702Sandre	return 1;
806122702Sandre}
807