ip_fastfwd.c revision 181803
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
30/*
31 * ip_fastforward gets its speed from processing the forwarded packet to
32 * completion (if_output on the other side) without any queues or netisr's.
33 * The receiving interface DMAs the packet into memory, the upper half of
34 * driver calls ip_fastforward, we do our routing table lookup and directly
35 * send it off to the outgoing interface, which DMAs the packet to the
36 * network card. The only part of the packet we touch with the CPU is the
37 * IP header (unless there are complex firewall rules touching other parts
38 * of the packet, but that is up to you). We are essentially limited by bus
39 * bandwidth and how fast the network card/driver can set up receives and
40 * transmits.
41 *
42 * We handle basic errors, IP header errors, checksum errors,
43 * destination unreachable, fragmentation and fragmentation needed and
44 * report them via ICMP to the sender.
45 *
46 * Else if something is not pure IPv4 unicast forwarding we fall back to
47 * the normal ip_input processing path. We should only be called from
48 * interfaces connected to the outside world.
49 *
50 * Firewalling is fully supported including divert, ipfw fwd and ipfilter
51 * ipnat and address rewrite.
52 *
53 * IPSEC is not supported if this host is a tunnel broker. IPSEC is
54 * supported for connections to/from local host.
55 *
56 * We try to do the least expensive (in CPU ops) checks and operations
57 * first to catch junk with as little overhead as possible.
58 *
59 * We take full advantage of hardware support for IP checksum and
60 * fragmentation offloading.
61 *
62 * We don't do ICMP redirect in the fast forwarding path. I have had my own
63 * cases where two core routers with Zebra routing suite would send millions
64 * ICMP redirects to connected hosts if the destination router was not the
65 * default gateway. In one case it was filling the routing table of a host
66 * with approximately 300.000 cloned redirect entries until it ran out of
67 * kernel memory. However the networking code proved very robust and it didn't
68 * crash or fail in other ways.
69 */
70
71/*
72 * Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which
73 * is being followed here.
74 */
75
76#include <sys/cdefs.h>
77__FBSDID("$FreeBSD: head/sys/netinet/ip_fastfwd.c 181803 2008-08-17 23:27:27Z bz $");
78
79#include "opt_ipfw.h"
80#include "opt_ipstealth.h"
81
82#include <sys/param.h>
83#include <sys/systm.h>
84#include <sys/kernel.h>
85#include <sys/malloc.h>
86#include <sys/mbuf.h>
87#include <sys/protosw.h>
88#include <sys/socket.h>
89#include <sys/sysctl.h>
90#include <sys/vimage.h>
91
92#include <net/pfil.h>
93#include <net/if.h>
94#include <net/if_types.h>
95#include <net/if_var.h>
96#include <net/if_dl.h>
97#include <net/route.h>
98
99#include <netinet/in.h>
100#include <netinet/in_systm.h>
101#include <netinet/in_var.h>
102#include <netinet/ip.h>
103#include <netinet/ip_var.h>
104#include <netinet/ip_icmp.h>
105#include <netinet/ip_options.h>
106
107#include <machine/in_cksum.h>
108
109static int ipfastforward_active = 0;
110SYSCTL_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW,
111    &ipfastforward_active, 0, "Enable fast IP forwarding");
112
113static struct sockaddr_in *
114ip_findroute(struct route *ro, struct in_addr dest, struct mbuf *m)
115{
116	struct sockaddr_in *dst;
117	struct rtentry *rt;
118
119	/*
120	 * Find route to destination.
121	 */
122	bzero(ro, sizeof(*ro));
123	dst = (struct sockaddr_in *)&ro->ro_dst;
124	dst->sin_family = AF_INET;
125	dst->sin_len = sizeof(*dst);
126	dst->sin_addr.s_addr = dest.s_addr;
127	in_rtalloc_ign(ro, RTF_CLONING, M_GETFIB(m));
128
129	/*
130	 * Route there and interface still up?
131	 */
132	rt = ro->ro_rt;
133	if (rt && (rt->rt_flags & RTF_UP) &&
134	    (rt->rt_ifp->if_flags & IFF_UP) &&
135	    (rt->rt_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
136		if (rt->rt_flags & RTF_GATEWAY)
137			dst = (struct sockaddr_in *)rt->rt_gateway;
138	} else {
139		V_ipstat.ips_noroute++;
140		V_ipstat.ips_cantforward++;
141		if (rt)
142			RTFREE(rt);
143		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
144		return NULL;
145	}
146	return dst;
147}
148
149/*
150 * Try to forward a packet based on the destination address.
151 * This is a fast path optimized for the plain forwarding case.
152 * If the packet is handled (and consumed) here then we return 1;
153 * otherwise 0 is returned and the packet should be delivered
154 * to ip_input for full processing.
155 */
156struct mbuf *
157ip_fastforward(struct mbuf *m)
158{
159	struct ip *ip;
160	struct mbuf *m0 = NULL;
161	struct route ro;
162	struct sockaddr_in *dst = NULL;
163	struct ifnet *ifp;
164	struct in_addr odest, dest;
165	u_short sum, ip_len;
166	int error = 0;
167	int hlen, mtu;
168#ifdef IPFIREWALL_FORWARD
169	struct m_tag *fwd_tag;
170#endif
171
172	/*
173	 * Are we active and forwarding packets?
174	 */
175	if (!V_ipfastforward_active || !V_ipforwarding)
176		return m;
177
178	M_ASSERTVALID(m);
179	M_ASSERTPKTHDR(m);
180
181	ro.ro_rt = NULL;
182
183	/*
184	 * Step 1: check for packet drop conditions (and sanity checks)
185	 */
186
187	/*
188	 * Is entire packet big enough?
189	 */
190	if (m->m_pkthdr.len < sizeof(struct ip)) {
191		V_ipstat.ips_tooshort++;
192		goto drop;
193	}
194
195	/*
196	 * Is first mbuf large enough for ip header and is header present?
197	 */
198	if (m->m_len < sizeof (struct ip) &&
199	   (m = m_pullup(m, sizeof (struct ip))) == NULL) {
200		V_ipstat.ips_toosmall++;
201		return NULL;	/* mbuf already free'd */
202	}
203
204	ip = mtod(m, struct ip *);
205
206	/*
207	 * Is it IPv4?
208	 */
209	if (ip->ip_v != IPVERSION) {
210		V_ipstat.ips_badvers++;
211		goto drop;
212	}
213
214	/*
215	 * Is IP header length correct and is it in first mbuf?
216	 */
217	hlen = ip->ip_hl << 2;
218	if (hlen < sizeof(struct ip)) {	/* minimum header length */
219		V_ipstat.ips_badlen++;
220		goto drop;
221	}
222	if (hlen > m->m_len) {
223		if ((m = m_pullup(m, hlen)) == NULL) {
224			V_ipstat.ips_badhlen++;
225			return NULL;	/* mbuf already free'd */
226		}
227		ip = mtod(m, struct ip *);
228	}
229
230	/*
231	 * Checksum correct?
232	 */
233	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED)
234		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
235	else {
236		if (hlen == sizeof(struct ip))
237			sum = in_cksum_hdr(ip);
238		else
239			sum = in_cksum(m, hlen);
240	}
241	if (sum) {
242		V_ipstat.ips_badsum++;
243		goto drop;
244	}
245
246	/*
247	 * Remember that we have checked the IP header and found it valid.
248	 */
249	m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
250
251	ip_len = ntohs(ip->ip_len);
252
253	/*
254	 * Is IP length longer than packet we have got?
255	 */
256	if (m->m_pkthdr.len < ip_len) {
257		V_ipstat.ips_tooshort++;
258		goto drop;
259	}
260
261	/*
262	 * Is packet longer than IP header tells us? If yes, truncate packet.
263	 */
264	if (m->m_pkthdr.len > ip_len) {
265		if (m->m_len == m->m_pkthdr.len) {
266			m->m_len = ip_len;
267			m->m_pkthdr.len = ip_len;
268		} else
269			m_adj(m, ip_len - m->m_pkthdr.len);
270	}
271
272	/*
273	 * Is packet from or to 127/8?
274	 */
275	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
276	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
277		V_ipstat.ips_badaddr++;
278		goto drop;
279	}
280
281#ifdef ALTQ
282	/*
283	 * Is packet dropped by traffic conditioner?
284	 */
285	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
286		goto drop;
287#endif
288
289	/*
290	 * Step 2: fallback conditions to normal ip_input path processing
291	 */
292
293	/*
294	 * Only IP packets without options
295	 */
296	if (ip->ip_hl != (sizeof(struct ip) >> 2)) {
297		if (ip_doopts == 1)
298			return m;
299		else if (ip_doopts == 2) {
300			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,
301				0, 0);
302			return NULL;	/* mbuf already free'd */
303		}
304		/* else ignore IP options and continue */
305	}
306
307	/*
308	 * Only unicast IP, not from loopback, no L2 or IP broadcast,
309	 * no multicast, no INADDR_ANY
310	 *
311	 * XXX: Probably some of these checks could be direct drop
312	 * conditions.  However it is not clear whether there are some
313	 * hacks or obscure behaviours which make it neccessary to
314	 * let ip_input handle it.  We play safe here and let ip_input
315	 * deal with it until it is proven that we can directly drop it.
316	 */
317	if ((m->m_flags & (M_BCAST|M_MCAST)) ||
318	    (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
319	    ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
320	    ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
321	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
322	    IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
323	    IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) ||
324	    IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
325	    ip->ip_src.s_addr == INADDR_ANY ||
326	    ip->ip_dst.s_addr == INADDR_ANY )
327		return m;
328
329	/*
330	 * Is it for a local address on this host?
331	 */
332	if (in_localip(ip->ip_dst))
333		return m;
334
335	V_ipstat.ips_total++;
336
337	/*
338	 * Step 3: incoming packet firewall processing
339	 */
340
341	/*
342	 * Convert to host representation
343	 */
344	ip->ip_len = ntohs(ip->ip_len);
345	ip->ip_off = ntohs(ip->ip_off);
346
347	odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;
348
349	/*
350	 * Run through list of ipfilter hooks for input packets
351	 */
352	if (!PFIL_HOOKED(&inet_pfil_hook))
353		goto passin;
354
355	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL) ||
356	    m == NULL)
357		goto drop;
358
359	M_ASSERTVALID(m);
360	M_ASSERTPKTHDR(m);
361
362	ip = mtod(m, struct ip *);	/* m may have changed by pfil hook */
363	dest.s_addr = ip->ip_dst.s_addr;
364
365	/*
366	 * Destination address changed?
367	 */
368	if (odest.s_addr != dest.s_addr) {
369		/*
370		 * Is it now for a local address on this host?
371		 */
372		if (in_localip(dest))
373			goto forwardlocal;
374		/*
375		 * Go on with new destination address
376		 */
377	}
378#ifdef IPFIREWALL_FORWARD
379	if (m->m_flags & M_FASTFWD_OURS) {
380		/*
381		 * ipfw changed it for a local address on this host.
382		 */
383		goto forwardlocal;
384	}
385#endif /* IPFIREWALL_FORWARD */
386
387passin:
388	/*
389	 * Step 4: decrement TTL and look up route
390	 */
391
392	/*
393	 * Check TTL
394	 */
395#ifdef IPSTEALTH
396	if (!V_ipstealth) {
397#endif
398	if (ip->ip_ttl <= IPTTLDEC) {
399		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
400		return NULL;	/* mbuf already free'd */
401	}
402
403	/*
404	 * Decrement the TTL and incrementally change the IP header checksum.
405	 * Don't bother doing this with hw checksum offloading, it's faster
406	 * doing it right here.
407	 */
408	ip->ip_ttl -= IPTTLDEC;
409	if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
410		ip->ip_sum -= ~htons(IPTTLDEC << 8);
411	else
412		ip->ip_sum += htons(IPTTLDEC << 8);
413#ifdef IPSTEALTH
414	}
415#endif
416
417	/*
418	 * Find route to destination.
419	 */
420	if ((dst = ip_findroute(&ro, dest, m)) == NULL)
421		return NULL;	/* icmp unreach already sent */
422	ifp = ro.ro_rt->rt_ifp;
423
424	/*
425	 * Immediately drop blackholed traffic, and directed broadcasts
426	 * for either the all-ones or all-zero subnet addresses on
427	 * locally attached networks.
428	 */
429	if ((ro.ro_rt->rt_flags & (RTF_BLACKHOLE|RTF_BROADCAST)) != 0)
430		goto drop;
431
432	/*
433	 * Step 5: outgoing firewall packet processing
434	 */
435
436	/*
437	 * Run through list of hooks for output packets.
438	 */
439	if (!PFIL_HOOKED(&inet_pfil_hook))
440		goto passout;
441
442	if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, NULL) || m == NULL) {
443		goto drop;
444	}
445
446	M_ASSERTVALID(m);
447	M_ASSERTPKTHDR(m);
448
449	ip = mtod(m, struct ip *);
450	dest.s_addr = ip->ip_dst.s_addr;
451
452	/*
453	 * Destination address changed?
454	 */
455#ifndef IPFIREWALL_FORWARD
456	if (odest.s_addr != dest.s_addr) {
457#else
458	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
459	if (odest.s_addr != dest.s_addr || fwd_tag != NULL) {
460#endif /* IPFIREWALL_FORWARD */
461		/*
462		 * Is it now for a local address on this host?
463		 */
464#ifndef IPFIREWALL_FORWARD
465		if (in_localip(dest)) {
466#else
467		if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) {
468#endif /* IPFIREWALL_FORWARD */
469forwardlocal:
470			/*
471			 * Return packet for processing by ip_input().
472			 * Keep host byte order as expected at ip_input's
473			 * "ours"-label.
474			 */
475			m->m_flags |= M_FASTFWD_OURS;
476			if (ro.ro_rt)
477				RTFREE(ro.ro_rt);
478			return m;
479		}
480		/*
481		 * Redo route lookup with new destination address
482		 */
483#ifdef IPFIREWALL_FORWARD
484		if (fwd_tag) {
485			dest.s_addr = ((struct sockaddr_in *)
486				    (fwd_tag + 1))->sin_addr.s_addr;
487			m_tag_delete(m, fwd_tag);
488		}
489#endif /* IPFIREWALL_FORWARD */
490		RTFREE(ro.ro_rt);
491		if ((dst = ip_findroute(&ro, dest, m)) == NULL)
492			return NULL;	/* icmp unreach already sent */
493		ifp = ro.ro_rt->rt_ifp;
494	}
495
496passout:
497	/*
498	 * Step 6: send off the packet
499	 */
500
501	/*
502	 * Check if route is dampned (when ARP is unable to resolve)
503	 */
504	if ((ro.ro_rt->rt_flags & RTF_REJECT) &&
505	    (ro.ro_rt->rt_rmx.rmx_expire == 0 ||
506	    time_uptime < ro.ro_rt->rt_rmx.rmx_expire)) {
507		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
508		goto consumed;
509	}
510
511#ifndef ALTQ
512	/*
513	 * Check if there is enough space in the interface queue
514	 */
515	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
516	    ifp->if_snd.ifq_maxlen) {
517		V_ipstat.ips_odropped++;
518		/* would send source quench here but that is depreciated */
519		goto drop;
520	}
521#endif
522
523	/*
524	 * Check if media link state of interface is not down
525	 */
526	if (ifp->if_link_state == LINK_STATE_DOWN) {
527		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
528		goto consumed;
529	}
530
531	/*
532	 * Check if packet fits MTU or if hardware will fragment for us
533	 */
534	if (ro.ro_rt->rt_rmx.rmx_mtu)
535		mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu);
536	else
537		mtu = ifp->if_mtu;
538
539	if (ip->ip_len <= mtu ||
540	    (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) {
541		/*
542		 * Restore packet header fields to original values
543		 */
544		ip->ip_len = htons(ip->ip_len);
545		ip->ip_off = htons(ip->ip_off);
546		/*
547		 * Send off the packet via outgoing interface
548		 */
549		error = (*ifp->if_output)(ifp, m,
550				(struct sockaddr *)dst, ro.ro_rt);
551	} else {
552		/*
553		 * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery
554		 */
555		if (ip->ip_off & IP_DF) {
556			V_ipstat.ips_cantfrag++;
557			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
558				0, mtu);
559			goto consumed;
560		} else {
561			/*
562			 * We have to fragment the packet
563			 */
564			m->m_pkthdr.csum_flags |= CSUM_IP;
565			/*
566			 * ip_fragment expects ip_len and ip_off in host byte
567			 * order but returns all packets in network byte order
568			 */
569			if (ip_fragment(ip, &m, mtu, ifp->if_hwassist,
570					(~ifp->if_hwassist & CSUM_DELAY_IP))) {
571				goto drop;
572			}
573			KASSERT(m != NULL, ("null mbuf and no error"));
574			/*
575			 * Send off the fragments via outgoing interface
576			 */
577			error = 0;
578			do {
579				m0 = m->m_nextpkt;
580				m->m_nextpkt = NULL;
581
582				error = (*ifp->if_output)(ifp, m,
583					(struct sockaddr *)dst, ro.ro_rt);
584				if (error)
585					break;
586			} while ((m = m0) != NULL);
587			if (error) {
588				/* Reclaim remaining fragments */
589				for (m = m0; m; m = m0) {
590					m0 = m->m_nextpkt;
591					m_freem(m);
592				}
593			} else
594				V_ipstat.ips_fragmented++;
595		}
596	}
597
598	if (error != 0)
599		V_ipstat.ips_odropped++;
600	else {
601		ro.ro_rt->rt_rmx.rmx_pksent++;
602		V_ipstat.ips_forward++;
603		V_ipstat.ips_fastforward++;
604	}
605consumed:
606	RTFREE(ro.ro_rt);
607	return NULL;
608drop:
609	if (m)
610		m_freem(m);
611	if (ro.ro_rt)
612		RTFREE(ro.ro_rt);
613	return NULL;
614}
615