ip_fastfwd.c revision 185088
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 185088 2008-11-19 09:39:34Z zec $");
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
109#ifdef VIMAGE_GLOBALS
110static int ipfastforward_active;
111#endif
112SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, fastforwarding,
113    CTLFLAG_RW, ipfastforward_active, 0, "Enable fast IP forwarding");
114
115static struct sockaddr_in *
116ip_findroute(struct route *ro, struct in_addr dest, struct mbuf *m)
117{
118	INIT_VNET_INET(curvnet);
119	struct sockaddr_in *dst;
120	struct rtentry *rt;
121
122	/*
123	 * Find route to destination.
124	 */
125	bzero(ro, sizeof(*ro));
126	dst = (struct sockaddr_in *)&ro->ro_dst;
127	dst->sin_family = AF_INET;
128	dst->sin_len = sizeof(*dst);
129	dst->sin_addr.s_addr = dest.s_addr;
130	in_rtalloc_ign(ro, RTF_CLONING, M_GETFIB(m));
131
132	/*
133	 * Route there and interface still up?
134	 */
135	rt = ro->ro_rt;
136	if (rt && (rt->rt_flags & RTF_UP) &&
137	    (rt->rt_ifp->if_flags & IFF_UP) &&
138	    (rt->rt_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
139		if (rt->rt_flags & RTF_GATEWAY)
140			dst = (struct sockaddr_in *)rt->rt_gateway;
141	} else {
142		V_ipstat.ips_noroute++;
143		V_ipstat.ips_cantforward++;
144		if (rt)
145			RTFREE(rt);
146		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
147		return NULL;
148	}
149	return dst;
150}
151
152/*
153 * Try to forward a packet based on the destination address.
154 * This is a fast path optimized for the plain forwarding case.
155 * If the packet is handled (and consumed) here then we return 1;
156 * otherwise 0 is returned and the packet should be delivered
157 * to ip_input for full processing.
158 */
159struct mbuf *
160ip_fastforward(struct mbuf *m)
161{
162	INIT_VNET_INET(curvnet);
163	struct ip *ip;
164	struct mbuf *m0 = NULL;
165	struct route ro;
166	struct sockaddr_in *dst = NULL;
167	struct ifnet *ifp;
168	struct in_addr odest, dest;
169	u_short sum, ip_len;
170	int error = 0;
171	int hlen, mtu;
172#ifdef IPFIREWALL_FORWARD
173	struct m_tag *fwd_tag;
174#endif
175
176	/*
177	 * Are we active and forwarding packets?
178	 */
179	if (!V_ipfastforward_active || !V_ipforwarding)
180		return m;
181
182	M_ASSERTVALID(m);
183	M_ASSERTPKTHDR(m);
184
185	ro.ro_rt = NULL;
186
187	/*
188	 * Step 1: check for packet drop conditions (and sanity checks)
189	 */
190
191	/*
192	 * Is entire packet big enough?
193	 */
194	if (m->m_pkthdr.len < sizeof(struct ip)) {
195		V_ipstat.ips_tooshort++;
196		goto drop;
197	}
198
199	/*
200	 * Is first mbuf large enough for ip header and is header present?
201	 */
202	if (m->m_len < sizeof (struct ip) &&
203	   (m = m_pullup(m, sizeof (struct ip))) == NULL) {
204		V_ipstat.ips_toosmall++;
205		return NULL;	/* mbuf already free'd */
206	}
207
208	ip = mtod(m, struct ip *);
209
210	/*
211	 * Is it IPv4?
212	 */
213	if (ip->ip_v != IPVERSION) {
214		V_ipstat.ips_badvers++;
215		goto drop;
216	}
217
218	/*
219	 * Is IP header length correct and is it in first mbuf?
220	 */
221	hlen = ip->ip_hl << 2;
222	if (hlen < sizeof(struct ip)) {	/* minimum header length */
223		V_ipstat.ips_badlen++;
224		goto drop;
225	}
226	if (hlen > m->m_len) {
227		if ((m = m_pullup(m, hlen)) == NULL) {
228			V_ipstat.ips_badhlen++;
229			return NULL;	/* mbuf already free'd */
230		}
231		ip = mtod(m, struct ip *);
232	}
233
234	/*
235	 * Checksum correct?
236	 */
237	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED)
238		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
239	else {
240		if (hlen == sizeof(struct ip))
241			sum = in_cksum_hdr(ip);
242		else
243			sum = in_cksum(m, hlen);
244	}
245	if (sum) {
246		V_ipstat.ips_badsum++;
247		goto drop;
248	}
249
250	/*
251	 * Remember that we have checked the IP header and found it valid.
252	 */
253	m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
254
255	ip_len = ntohs(ip->ip_len);
256
257	/*
258	 * Is IP length longer than packet we have got?
259	 */
260	if (m->m_pkthdr.len < ip_len) {
261		V_ipstat.ips_tooshort++;
262		goto drop;
263	}
264
265	/*
266	 * Is packet longer than IP header tells us? If yes, truncate packet.
267	 */
268	if (m->m_pkthdr.len > ip_len) {
269		if (m->m_len == m->m_pkthdr.len) {
270			m->m_len = ip_len;
271			m->m_pkthdr.len = ip_len;
272		} else
273			m_adj(m, ip_len - m->m_pkthdr.len);
274	}
275
276	/*
277	 * Is packet from or to 127/8?
278	 */
279	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
280	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
281		V_ipstat.ips_badaddr++;
282		goto drop;
283	}
284
285#ifdef ALTQ
286	/*
287	 * Is packet dropped by traffic conditioner?
288	 */
289	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
290		goto drop;
291#endif
292
293	/*
294	 * Step 2: fallback conditions to normal ip_input path processing
295	 */
296
297	/*
298	 * Only IP packets without options
299	 */
300	if (ip->ip_hl != (sizeof(struct ip) >> 2)) {
301		if (ip_doopts == 1)
302			return m;
303		else if (ip_doopts == 2) {
304			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,
305				0, 0);
306			return NULL;	/* mbuf already free'd */
307		}
308		/* else ignore IP options and continue */
309	}
310
311	/*
312	 * Only unicast IP, not from loopback, no L2 or IP broadcast,
313	 * no multicast, no INADDR_ANY
314	 *
315	 * XXX: Probably some of these checks could be direct drop
316	 * conditions.  However it is not clear whether there are some
317	 * hacks or obscure behaviours which make it neccessary to
318	 * let ip_input handle it.  We play safe here and let ip_input
319	 * deal with it until it is proven that we can directly drop it.
320	 */
321	if ((m->m_flags & (M_BCAST|M_MCAST)) ||
322	    (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
323	    ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
324	    ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
325	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
326	    IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
327	    IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) ||
328	    IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
329	    ip->ip_src.s_addr == INADDR_ANY ||
330	    ip->ip_dst.s_addr == INADDR_ANY )
331		return m;
332
333	/*
334	 * Is it for a local address on this host?
335	 */
336	if (in_localip(ip->ip_dst))
337		return m;
338
339	V_ipstat.ips_total++;
340
341	/*
342	 * Step 3: incoming packet firewall processing
343	 */
344
345	/*
346	 * Convert to host representation
347	 */
348	ip->ip_len = ntohs(ip->ip_len);
349	ip->ip_off = ntohs(ip->ip_off);
350
351	odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;
352
353	/*
354	 * Run through list of ipfilter hooks for input packets
355	 */
356	if (!PFIL_HOOKED(&inet_pfil_hook))
357		goto passin;
358
359	if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL) ||
360	    m == NULL)
361		goto drop;
362
363	M_ASSERTVALID(m);
364	M_ASSERTPKTHDR(m);
365
366	ip = mtod(m, struct ip *);	/* m may have changed by pfil hook */
367	dest.s_addr = ip->ip_dst.s_addr;
368
369	/*
370	 * Destination address changed?
371	 */
372	if (odest.s_addr != dest.s_addr) {
373		/*
374		 * Is it now for a local address on this host?
375		 */
376		if (in_localip(dest))
377			goto forwardlocal;
378		/*
379		 * Go on with new destination address
380		 */
381	}
382#ifdef IPFIREWALL_FORWARD
383	if (m->m_flags & M_FASTFWD_OURS) {
384		/*
385		 * ipfw changed it for a local address on this host.
386		 */
387		goto forwardlocal;
388	}
389#endif /* IPFIREWALL_FORWARD */
390
391passin:
392	/*
393	 * Step 4: decrement TTL and look up route
394	 */
395
396	/*
397	 * Check TTL
398	 */
399#ifdef IPSTEALTH
400	if (!V_ipstealth) {
401#endif
402	if (ip->ip_ttl <= IPTTLDEC) {
403		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
404		return NULL;	/* mbuf already free'd */
405	}
406
407	/*
408	 * Decrement the TTL and incrementally change the IP header checksum.
409	 * Don't bother doing this with hw checksum offloading, it's faster
410	 * doing it right here.
411	 */
412	ip->ip_ttl -= IPTTLDEC;
413	if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
414		ip->ip_sum -= ~htons(IPTTLDEC << 8);
415	else
416		ip->ip_sum += htons(IPTTLDEC << 8);
417#ifdef IPSTEALTH
418	}
419#endif
420
421	/*
422	 * Find route to destination.
423	 */
424	if ((dst = ip_findroute(&ro, dest, m)) == NULL)
425		return NULL;	/* icmp unreach already sent */
426	ifp = ro.ro_rt->rt_ifp;
427
428	/*
429	 * Immediately drop blackholed traffic, and directed broadcasts
430	 * for either the all-ones or all-zero subnet addresses on
431	 * locally attached networks.
432	 */
433	if ((ro.ro_rt->rt_flags & (RTF_BLACKHOLE|RTF_BROADCAST)) != 0)
434		goto drop;
435
436	/*
437	 * Step 5: outgoing firewall packet processing
438	 */
439
440	/*
441	 * Run through list of hooks for output packets.
442	 */
443	if (!PFIL_HOOKED(&inet_pfil_hook))
444		goto passout;
445
446	if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, NULL) || m == NULL) {
447		goto drop;
448	}
449
450	M_ASSERTVALID(m);
451	M_ASSERTPKTHDR(m);
452
453	ip = mtod(m, struct ip *);
454	dest.s_addr = ip->ip_dst.s_addr;
455
456	/*
457	 * Destination address changed?
458	 */
459#ifndef IPFIREWALL_FORWARD
460	if (odest.s_addr != dest.s_addr) {
461#else
462	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
463	if (odest.s_addr != dest.s_addr || fwd_tag != NULL) {
464#endif /* IPFIREWALL_FORWARD */
465		/*
466		 * Is it now for a local address on this host?
467		 */
468#ifndef IPFIREWALL_FORWARD
469		if (in_localip(dest)) {
470#else
471		if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) {
472#endif /* IPFIREWALL_FORWARD */
473forwardlocal:
474			/*
475			 * Return packet for processing by ip_input().
476			 * Keep host byte order as expected at ip_input's
477			 * "ours"-label.
478			 */
479			m->m_flags |= M_FASTFWD_OURS;
480			if (ro.ro_rt)
481				RTFREE(ro.ro_rt);
482			return m;
483		}
484		/*
485		 * Redo route lookup with new destination address
486		 */
487#ifdef IPFIREWALL_FORWARD
488		if (fwd_tag) {
489			dest.s_addr = ((struct sockaddr_in *)
490				    (fwd_tag + 1))->sin_addr.s_addr;
491			m_tag_delete(m, fwd_tag);
492		}
493#endif /* IPFIREWALL_FORWARD */
494		RTFREE(ro.ro_rt);
495		if ((dst = ip_findroute(&ro, dest, m)) == NULL)
496			return NULL;	/* icmp unreach already sent */
497		ifp = ro.ro_rt->rt_ifp;
498	}
499
500passout:
501	/*
502	 * Step 6: send off the packet
503	 */
504
505	/*
506	 * Check if route is dampned (when ARP is unable to resolve)
507	 */
508	if ((ro.ro_rt->rt_flags & RTF_REJECT) &&
509	    (ro.ro_rt->rt_rmx.rmx_expire == 0 ||
510	    time_uptime < ro.ro_rt->rt_rmx.rmx_expire)) {
511		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
512		goto consumed;
513	}
514
515#ifndef ALTQ
516	/*
517	 * Check if there is enough space in the interface queue
518	 */
519	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
520	    ifp->if_snd.ifq_maxlen) {
521		V_ipstat.ips_odropped++;
522		/* would send source quench here but that is depreciated */
523		goto drop;
524	}
525#endif
526
527	/*
528	 * Check if media link state of interface is not down
529	 */
530	if (ifp->if_link_state == LINK_STATE_DOWN) {
531		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
532		goto consumed;
533	}
534
535	/*
536	 * Check if packet fits MTU or if hardware will fragment for us
537	 */
538	if (ro.ro_rt->rt_rmx.rmx_mtu)
539		mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu);
540	else
541		mtu = ifp->if_mtu;
542
543	if (ip->ip_len <= mtu ||
544	    (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) {
545		/*
546		 * Restore packet header fields to original values
547		 */
548		ip->ip_len = htons(ip->ip_len);
549		ip->ip_off = htons(ip->ip_off);
550		/*
551		 * Send off the packet via outgoing interface
552		 */
553		error = (*ifp->if_output)(ifp, m,
554				(struct sockaddr *)dst, ro.ro_rt);
555	} else {
556		/*
557		 * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery
558		 */
559		if (ip->ip_off & IP_DF) {
560			V_ipstat.ips_cantfrag++;
561			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
562				0, mtu);
563			goto consumed;
564		} else {
565			/*
566			 * We have to fragment the packet
567			 */
568			m->m_pkthdr.csum_flags |= CSUM_IP;
569			/*
570			 * ip_fragment expects ip_len and ip_off in host byte
571			 * order but returns all packets in network byte order
572			 */
573			if (ip_fragment(ip, &m, mtu, ifp->if_hwassist,
574					(~ifp->if_hwassist & CSUM_DELAY_IP))) {
575				goto drop;
576			}
577			KASSERT(m != NULL, ("null mbuf and no error"));
578			/*
579			 * Send off the fragments via outgoing interface
580			 */
581			error = 0;
582			do {
583				m0 = m->m_nextpkt;
584				m->m_nextpkt = NULL;
585
586				error = (*ifp->if_output)(ifp, m,
587					(struct sockaddr *)dst, ro.ro_rt);
588				if (error)
589					break;
590			} while ((m = m0) != NULL);
591			if (error) {
592				/* Reclaim remaining fragments */
593				for (m = m0; m; m = m0) {
594					m0 = m->m_nextpkt;
595					m_freem(m);
596				}
597			} else
598				V_ipstat.ips_fragmented++;
599		}
600	}
601
602	if (error != 0)
603		V_ipstat.ips_odropped++;
604	else {
605		ro.ro_rt->rt_rmx.rmx_pksent++;
606		V_ipstat.ips_forward++;
607		V_ipstat.ips_fastforward++;
608	}
609consumed:
610	RTFREE(ro.ro_rt);
611	return NULL;
612drop:
613	if (m)
614		m_freem(m);
615	if (ro.ro_rt)
616		RTFREE(ro.ro_rt);
617	return NULL;
618}
619