ip_fastfwd.c revision 125784
1/*
2 * Copyright (c) 2003 Andre Oppermann, Internet Business Solutions AG
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 *    products derived from this software without specific prior written
15 *    permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/netinet/ip_fastfwd.c 125784 2004-02-13 19:14:16Z mlaier $
30 */
31
32/*
33 * ip_fastforward gets its speed from processing the forwarded packet to
34 * completion (if_output on the other side) without any queues or netisr's.
35 * The receiving interface DMAs the packet into memory, the upper half of
36 * driver calls ip_fastforward, we do our routing table lookup and directly
37 * send it off to the outgoing interface which DMAs the packet to the
38 * network card. The only part of the packet we touch with the CPU is the
39 * IP header (unless there are complex firewall rules touching other parts
40 * of the packet, but that is up to you). We are essentially limited by bus
41 * bandwidth and how fast the network card/driver can set up receives and
42 * transmits.
43 *
44 * We handle basic errors, ip header errors, checksum errors,
45 * destination unreachable, fragmentation and fragmentation needed and
46 * report them via icmp to the sender.
47 *
48 * Else if something is not pure IPv4 unicast forwarding we fall back to
49 * the normal ip_input processing path. We should only be called from
50 * interfaces connected to the outside world.
51 *
52 * Firewalling is fully supported including divert, ipfw fwd and ipfilter
53 * ipnat and address rewrite.
54 *
55 * IPSEC is not supported if this host is a tunnel broker. IPSEC is
56 * supported for connections to/from local host.
57 *
58 * We try to do the least expensive (in CPU ops) checks and operations
59 * first to catch junk with as little overhead as possible.
60 *
61 * We take full advantage of hardware support for ip checksum and
62 * fragmentation offloading.
63 *
64 * We don't do ICMP redirect in the fast forwarding path. I have had my own
65 * cases where two core routers with Zebra routing suite would send millions
66 * ICMP redirects to connected hosts if the router to dest was not the default
67 * gateway. In one case it was filling the routing table of a host with close
68 * 300'000 cloned redirect entries until it ran out of kernel memory. However
69 * the networking code proved very robust and it didn't crash or went ill
70 * otherwise.
71 */
72
73/*
74 * Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which
75 * is being followed here.
76 */
77
78#include "opt_ipfw.h"
79#include "opt_ipdn.h"
80#include "opt_ipdivert.h"
81#include "opt_ipfilter.h"
82#include "opt_ipstealth.h"
83#include "opt_mac.h"
84#include "opt_pfil_hooks.h"
85
86#include <sys/param.h>
87#include <sys/systm.h>
88#include <sys/kernel.h>
89#include <sys/mac.h>
90#include <sys/malloc.h>
91#include <sys/mbuf.h>
92#include <sys/protosw.h>
93#include <sys/socket.h>
94#include <sys/sysctl.h>
95
96#include <net/pfil.h>
97#include <net/if.h>
98#include <net/if_types.h>
99#include <net/if_var.h>
100#include <net/if_dl.h>
101#include <net/route.h>
102
103#include <netinet/in.h>
104#include <netinet/in_systm.h>
105#include <netinet/in_var.h>
106#include <netinet/ip.h>
107#include <netinet/ip_var.h>
108#include <netinet/ip_icmp.h>
109
110#include <machine/in_cksum.h>
111
112#include <netinet/ip_fw.h>
113#include <netinet/ip_divert.h>
114#include <netinet/ip_dummynet.h>
115
116static int ipfastforward_active = 0;
117SYSCTL_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW,
118    &ipfastforward_active, 0, "Enable fast IP forwarding");
119
120/*
121 * Try to forward a packet based on the destination address.
122 * This is a fast path optimized for the plain forwarding case.
123 * If the packet is handled (and consumed) here then we return 1;
124 * otherwise 0 is returned and the packet should be delivered
125 * to ip_input for full processing.
126 */
127int
128ip_fastforward(struct mbuf *m)
129{
130	struct ip *ip;
131	struct mbuf *m0 = NULL;
132#ifdef IPDIVERT
133	struct ip *tip;
134	struct mbuf *teem = NULL;
135#endif
136	struct m_tag *mtag;
137	struct route ro;
138	struct sockaddr_in *dst = NULL;
139	struct in_ifaddr *ia = NULL;
140	struct ifaddr *ifa = NULL;
141	struct ifnet *ifp = NULL;
142	struct ip_fw_args args;
143	in_addr_t odest, dest;
144	u_short sum;
145	int error = 0;
146	int hlen, ipfw, mtu;
147
148	/*
149	 * Are we active and forwarding packets?
150	 */
151	if (!ipfastforward_active || !ipforwarding)
152		return 0;
153
154	M_ASSERTVALID(m);
155	M_ASSERTPKTHDR(m);
156
157	/*
158	 * Step 1: check for packet drop conditions (and sanity checks)
159	 */
160
161	/*
162	 * Is entire packet big enough?
163	 */
164	if (m->m_pkthdr.len < sizeof(struct ip)) {
165		ipstat.ips_tooshort++;
166		goto drop;
167	}
168
169	/*
170	 * Is first mbuf large enough for ip header and is header present?
171	 */
172	if (m->m_len < sizeof (struct ip) &&
173	   (m = m_pullup(m, sizeof (struct ip))) == 0) {
174		ipstat.ips_toosmall++;
175		goto drop;
176	}
177
178	ip = mtod(m, struct ip *);
179
180	/*
181	 * Is it IPv4?
182	 */
183	if (ip->ip_v != IPVERSION) {
184		ipstat.ips_badvers++;
185		goto drop;
186	}
187
188	/*
189	 * Is IP header length correct and is it in first mbuf?
190	 */
191	hlen = ip->ip_hl << 2;
192	if (hlen < sizeof(struct ip)) {	/* minimum header length */
193		ipstat.ips_badlen++;
194		goto drop;
195	}
196	if (hlen > m->m_len) {
197		if ((m = m_pullup(m, hlen)) == 0) {
198			ipstat.ips_badhlen++;
199			goto drop;
200		}
201		ip = mtod(m, struct ip *);
202	}
203
204	/*
205	 * Checksum correct?
206	 */
207	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED)
208		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
209	else {
210		if (hlen == sizeof(struct ip))
211			sum = in_cksum_hdr(ip);
212		else
213			sum = in_cksum(m, hlen);
214	}
215	if (sum) {
216		ipstat.ips_badsum++;
217		goto drop;
218	}
219	m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
220
221	/*
222	 * Convert to host representation
223	 */
224	ip->ip_len = ntohs(ip->ip_len);
225	ip->ip_off = ntohs(ip->ip_off);
226
227	/*
228	 * Is IP length longer than packet we have got?
229	 */
230	if (m->m_pkthdr.len < ip->ip_len) {
231		ipstat.ips_tooshort++;
232		goto drop;
233	}
234
235	/*
236	 * Is packet longer than IP header tells us? If yes, truncate packet.
237	 */
238	if (m->m_pkthdr.len > ip->ip_len) {
239		if (m->m_len == m->m_pkthdr.len) {
240			m->m_len = ip->ip_len;
241			m->m_pkthdr.len = ip->ip_len;
242		} else
243			m_adj(m, ip->ip_len - m->m_pkthdr.len);
244	}
245
246	/*
247	 * Is packet from or to 127/8?
248	 */
249	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
250	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
251		ipstat.ips_badaddr++;
252		goto drop;
253	}
254
255	/*
256	 * Step 2: fallback conditions to normal ip_input path processing
257	 */
258
259	/*
260	 * Only IP packets without options
261	 */
262	if (ip->ip_hl != (sizeof(struct ip) >> 2))
263		goto fallback;
264
265	/*
266	 * Only unicast IP, not from loopback, no L2 or IP broadcast,
267	 * no multicast, no INADDR_ANY
268	 *
269	 * XXX: Probably some of these checks could be direct drop
270	 * conditions.  However it is not clear whether there are some
271	 * hacks or obscure behaviours which make it neccessary to
272	 * let ip_input handle it.  We play safe here and let ip_input
273	 * deal with it until it is proven that we can directly drop it.
274	 */
275	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
276	    ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
277	    ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
278	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
279	    IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
280	    ip->ip_dst.s_addr == INADDR_ANY )
281		goto fallback;
282
283	/*
284	 * Is it for a local address on this host?
285	 */
286	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
287		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
288			goto fallback;
289	}
290
291	/*
292	 * Or is it for a local IP broadcast address on this host?
293	 */
294	if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
295	        TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
296			if (ifa->ifa_addr->sa_family != AF_INET)
297				continue;
298			ia = ifatoia(ifa);
299			if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
300				goto fallback;
301			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
302			    ip->ip_dst.s_addr)
303				goto fallback;
304			continue;
305fallback:
306			/* return packet back to netisr for slow processing */
307			ip->ip_len = htons(ip->ip_len);
308			ip->ip_off = htons(ip->ip_off);
309			return 0;
310		}
311	}
312	ipstat.ips_total++;
313
314	/*
315	 * Step 3: incoming packet firewall processing
316	 */
317
318	odest = dest = ip->ip_dst.s_addr;
319#ifdef PFIL_HOOKS
320	/*
321	 * Run through list of ipfilter hooks for input packets
322	 */
323	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN) ||
324	    m == NULL)
325		return 1;
326
327	M_ASSERTVALID(m);
328	M_ASSERTPKTHDR(m);
329
330	ip = mtod(m, struct ip *);	/* m may have changed by pfil hook */
331	dest = ip->ip_dst.s_addr;
332#endif
333
334	/*
335	 * Run through ipfw for input packets
336	 */
337	if (fw_enable && IPFW_LOADED) {
338		bzero(&args, sizeof(args));
339		args.m = m;
340
341		ipfw = ip_fw_chk_ptr(&args);
342		m = args.m;
343
344		M_ASSERTVALID(m);
345		M_ASSERTPKTHDR(m);
346
347		/*
348		 * Packet denied, drop it
349		 */
350		if ((ipfw & IP_FW_PORT_DENY_FLAG) || m == NULL)
351			goto drop;
352		/*
353		 * Send packet to the appropriate pipe
354		 */
355		if (DUMMYNET_LOADED && (ipfw & IP_FW_PORT_DYNT_FLAG) != 0) {
356			ip_dn_io_ptr(m, ipfw & 0xffff, DN_TO_IP_IN, &args);
357			return 1;
358		}
359#ifdef IPDIVERT
360		/*
361		 * Divert packet
362		 */
363		if (ipfw != 0 && (ipfw & IP_FW_PORT_DYNT_FLAG) == 0) {
364			/*
365			 * See if this is a fragment
366			 */
367			if (ip->ip_off & (IP_MF | IP_OFFMASK))
368				goto droptoours;
369			/*
370			 * Tee packet
371			 */
372			if ((ipfw & IP_FW_PORT_TEE_FLAG) != 0)
373				teem = divert_clone(m);
374			else
375				teem = m;
376			if (teem == NULL)
377				goto passin;
378
379			/*
380			 * Delayed checksums are not compatible
381			 */
382			if (teem->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
383				in_delayed_cksum(teem);
384				teem->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
385			}
386			/*
387			 * Restore packet header fields to original values
388			 */
389			tip = mtod(teem, struct ip *);
390			tip->ip_len = htons(tip->ip_len);
391			tip->ip_off = htons(tip->ip_off);
392			/*
393			 * Deliver packet to divert input routine
394			 */
395			divert_packet(teem, 0);
396			/*
397			 * If this was not tee, we are done
398			 */
399			if ((ipfw & IP_FW_PORT_TEE_FLAG) == 0)
400				return 1;
401			/* Continue if it was tee */
402			goto passin;
403		}
404#endif
405		if (ipfw == 0 && args.next_hop != NULL) {
406			dest = args.next_hop->sin_addr.s_addr;
407			goto passin;
408		}
409		/*
410		 * Let through or not?
411		 */
412		if (ipfw != 0)
413			goto drop;
414	}
415passin:
416	ip = mtod(m, struct ip *);	/* if m changed during fw processing */
417
418	/*
419	 * Destination address changed?
420	 */
421	if (odest != dest) {
422		/*
423		 * Is it now for a local address on this host?
424		 */
425		LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
426			if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
427				goto forwardlocal;
428		}
429		/*
430		 * Go on with new destination address
431		 */
432	}
433
434	/*
435	 * Step 4: decrement TTL and look up route
436	 */
437
438	/*
439	 * Check TTL
440	 */
441#ifdef IPSTEALTH
442	if (!ipstealth) {
443#endif
444	if (ip->ip_ttl <= IPTTLDEC) {
445		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, NULL);
446		return 1;
447	}
448
449	/*
450	 * Decrement the TTL and incrementally change the checksum.
451	 * Don't bother doing this with hw checksum offloading.
452	 */
453	ip->ip_ttl -= IPTTLDEC;
454	if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
455		ip->ip_sum -= ~htons(IPTTLDEC << 8);
456	else
457		ip->ip_sum += htons(IPTTLDEC << 8);
458#ifdef IPSTEALTH
459	}
460#endif
461
462	/*
463	 * Find route to destination.
464	 */
465	bzero(&ro, sizeof(ro));
466	dst = (struct sockaddr_in *)&ro.ro_dst;
467	dst->sin_family = AF_INET;
468	dst->sin_len = sizeof(*dst);
469	dst->sin_addr.s_addr = dest;
470	rtalloc_ign(&ro, RTF_CLONING);
471
472	/*
473	 * Route there and interface still up?
474	 */
475	if (ro.ro_rt &&
476	    (ro.ro_rt->rt_flags & RTF_UP) &&
477	    (ro.ro_rt->rt_ifp->if_flags & IFF_UP)) {
478		ia = ifatoia(ro.ro_rt->rt_ifa);
479		ifp = ro.ro_rt->rt_ifp;
480		if (ro.ro_rt->rt_flags & RTF_GATEWAY)
481			dst = (struct sockaddr_in *)ro.ro_rt->rt_gateway;
482	} else {
483		ipstat.ips_noroute++;
484		ipstat.ips_cantforward++;
485		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL);
486		if (ro.ro_rt)
487			RTFREE(ro.ro_rt);
488		return 1;
489	}
490
491	/*
492	 * Step 5: outgoing firewall packet processing
493	 */
494
495#ifdef PFIL_HOOKS
496	/*
497	 * Run through list of hooks for output packets.
498	 */
499	if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT) || m == NULL) {
500		RTFREE(ro.ro_rt);
501		return 1;
502	}
503
504	M_ASSERTVALID(m);
505	M_ASSERTPKTHDR(m);
506
507	ip = mtod(m, struct ip *);
508	dest = ip->ip_dst.s_addr;
509#endif
510	if (fw_enable && IPFW_LOADED && !args.next_hop) {
511		bzero(&args, sizeof(args));
512		args.m = m;
513		args.oif = ifp;
514
515		ipfw = ip_fw_chk_ptr(&args);
516		m = args.m;
517
518		M_ASSERTVALID(m);
519		M_ASSERTPKTHDR(m);
520
521		if ((ipfw & IP_FW_PORT_DENY_FLAG) || m == NULL) {
522			RTFREE(ro.ro_rt);
523			goto drop;
524		}
525		if (DUMMYNET_LOADED && (ipfw & IP_FW_PORT_DYNT_FLAG) != 0) {
526			/*
527			 * XXX note: if the ifp or rt entry are deleted
528			 * while a pkt is in dummynet, we are in trouble!
529			 */
530			args.ro = &ro;		/* dummynet does not save it */
531			args.dst = dst;
532
533			ip_dn_io_ptr(m, ipfw & 0xffff, DN_TO_IP_OUT, &args);
534			RTFREE(ro.ro_rt);
535			return 1;
536		}
537#ifdef IPDIVERT
538		if (ipfw != 0 && (ipfw & IP_FW_PORT_DYNT_FLAG) == 0) {
539			/*
540			 * See if this is a fragment
541			 */
542			if (ip->ip_off & (IP_MF | IP_OFFMASK))
543				goto droptoours;
544			/*
545			 * Tee packet
546			 */
547			if ((ipfw & IP_FW_PORT_TEE_FLAG) != 0)
548				teem = divert_clone(m);
549			else
550				teem = m;
551			if (teem == NULL)
552				goto passout;
553
554			/*
555			 * Delayed checksums are not compatible with divert
556			 */
557			if (teem->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
558				in_delayed_cksum(teem);
559				teem->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
560			}
561			/*
562			 * Restore packet header fields to original values
563			 */
564			tip = mtod(teem, struct ip *);
565			tip->ip_len = htons(tip->ip_len);
566			tip->ip_off = htons(tip->ip_off);
567			/*
568			 * Deliver packet to divert input routine
569			 */
570			divert_packet(teem, 0);
571			/*
572			 * If this was not tee, we are done
573			 */
574			if ((ipfw & IP_FW_PORT_TEE_FLAG) == 0) {
575				RTFREE(ro.ro_rt);
576				return 1;
577			}
578			/* Continue if it was tee */
579			goto passout;
580		}
581#endif
582		if (ipfw == 0 && args.next_hop != NULL) {
583			dest = args.next_hop->sin_addr.s_addr;
584			goto passout;
585		}
586		/*
587		 * Let through or not?
588		 */
589		if (ipfw != 0)
590			goto drop;
591	}
592passout:
593	ip = mtod(m, struct ip *);
594
595	/*
596	 * Destination address changed?
597	 */
598	if (odest != dest) {
599		/*
600		 * Is it now for a local address on this host?
601		 */
602		LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
603			if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) {
604forwardlocal:
605				if (args.next_hop) {
606					mtag = m_tag_get(PACKET_TAG_IPFORWARD,
607						   sizeof(struct sockaddr_in *),
608						   M_NOWAIT);
609					if (mtag == NULL) {
610						/* XXX statistic */
611						if (ro.ro_rt)
612							RTFREE(ro.ro_rt);
613						goto drop;
614					}
615					*(struct sockaddr_in **)(mtag+1) =
616						args.next_hop;
617					m_tag_prepend(m, mtag);
618				}
619#ifdef IPDIVERT
620droptoours:	/* Used for DIVERT */
621#endif
622				/* NB: ip_input understands this */
623				m->m_flags |= M_FASTFWD_OURS;
624
625				/* ip still points to the real packet */
626				ip->ip_len = htons(ip->ip_len);
627				ip->ip_off = htons(ip->ip_off);
628
629				/*
630				 * Return packet for processing by ip_input
631				 */
632				if (ro.ro_rt)
633					RTFREE(ro.ro_rt);
634				return 0;
635			}
636		}
637		/*
638		 * Redo route lookup with new destination address
639		 */
640		RTFREE(ro.ro_rt);
641		bzero(&ro, sizeof(ro));
642		dst = (struct sockaddr_in *)&ro.ro_dst;
643		dst->sin_family = AF_INET;
644		dst->sin_len = sizeof(*dst);
645		dst->sin_addr.s_addr = dest;
646		rtalloc_ign(&ro, RTF_CLONING);
647
648		/*
649		 * Route there and interface still up?
650		 */
651		if (ro.ro_rt &&
652		    (ro.ro_rt->rt_flags & RTF_UP) &&
653		    (ro.ro_rt->rt_ifp->if_flags & IFF_UP)) {
654			ia = ifatoia(ro.ro_rt->rt_ifa);
655			ifp = ro.ro_rt->rt_ifp;
656			if (ro.ro_rt->rt_flags & RTF_GATEWAY)
657				dst = (struct sockaddr_in *)ro.ro_rt->rt_gateway;
658		} else {
659			ipstat.ips_noroute++;
660			ipstat.ips_cantforward++;
661			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL);
662			if (ro.ro_rt)
663				RTFREE(ro.ro_rt);
664			return 1;
665		}
666	}
667
668	/*
669	 * Step 6: send off the packet
670	 */
671
672	/*
673	 * Check if packet fits MTU or if hardware will fragement for us
674	 */
675	if (ro.ro_rt->rt_rmx.rmx_mtu)
676		mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu);
677	else
678		mtu = ifp->if_mtu;
679
680	if (ip->ip_len <= mtu ||
681	    (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) {
682		/*
683		 * Restore packet header fields to original values
684		 */
685		ip->ip_len = htons(ip->ip_len);
686		ip->ip_off = htons(ip->ip_off);
687		/*
688		 * Send off the packet via outgoing interface
689		 */
690		error = (*ifp->if_output)(ifp, m,
691				(struct sockaddr *)dst, ro.ro_rt);
692		if (ia) {
693			ia->ia_ifa.if_opackets++;
694			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
695		}
696	} else {
697		/*
698		 * Handle EMSGSIZE with icmp reply
699		 * needfrag for TCP MTU discovery
700		 */
701		if (ip->ip_off & IP_DF) {
702			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
703				0, ifp);
704			ipstat.ips_cantfrag++;
705			RTFREE(ro.ro_rt);
706			return 1;
707		} else {
708			/*
709			 * We have to fragement the packet
710			 */
711			m->m_pkthdr.csum_flags |= CSUM_IP;
712			if (ip_fragment(ip, &m, mtu, ifp->if_hwassist,
713					(~ifp->if_hwassist & CSUM_DELAY_IP))) {
714				RTFREE(ro.ro_rt);
715				goto drop;
716			}
717			KASSERT(m != NULL, ("null mbuf and no error"));
718			/*
719			 * Send off the fragments via outgoing interface
720			 */
721			error = 0;
722			do {
723				m0 = m->m_nextpkt;
724				m->m_nextpkt = NULL;
725
726				error = (*ifp->if_output)(ifp, m,
727					(struct sockaddr *)dst, ro.ro_rt);
728				if (error)
729					break;
730			} while ((m = m0) != NULL);
731			if (error) {
732				/* Reclaim remaining fragments */
733				for (; m; m = m0) {
734					m0 = m->m_nextpkt;
735					m->m_nextpkt = NULL;
736					m_freem(m);
737				}
738			} else
739				ipstat.ips_fragmented++;
740		}
741	}
742
743	if (error != 0)
744		ipstat.ips_odropped++;
745	else {
746		ro.ro_rt->rt_rmx.rmx_pksent++;
747		ipstat.ips_forward++;
748		ipstat.ips_fastforward++;
749	}
750	RTFREE(ro.ro_rt);
751	return 1;
752drop:
753	if (m)
754		m_freem(m);
755	return 1;
756}
757