xref: /freebsd/sys/netinet/ip_fastfwd.c (revision 396c556d77189a5c474d35cec6f44a762e310b7d)
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$");
78 
79 #include "opt_ipstealth.h"
80 
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
84 #include <sys/malloc.h>
85 #include <sys/mbuf.h>
86 #include <sys/protosw.h>
87 #include <sys/sdt.h>
88 #include <sys/socket.h>
89 #include <sys/sysctl.h>
90 
91 #include <net/pfil.h>
92 #include <net/if.h>
93 #include <net/if_types.h>
94 #include <net/if_var.h>
95 #include <net/if_dl.h>
96 #include <net/route.h>
97 #include <net/vnet.h>
98 
99 #include <netinet/in.h>
100 #include <netinet/in_fib.h>
101 #include <netinet/in_kdtrace.h>
102 #include <netinet/in_systm.h>
103 #include <netinet/in_var.h>
104 #include <netinet/ip.h>
105 #include <netinet/ip_var.h>
106 #include <netinet/ip_icmp.h>
107 #include <netinet/ip_options.h>
108 
109 #include <machine/in_cksum.h>
110 
111 static int
112 ip_findroute(struct nhop4_basic *pnh, struct in_addr dest, struct mbuf *m)
113 {
114 
115 	bzero(pnh, sizeof(*pnh));
116 	if (fib4_lookup_nh_basic(M_GETFIB(m), dest, 0, 0, pnh) != 0) {
117 		IPSTAT_INC(ips_noroute);
118 		IPSTAT_INC(ips_cantforward);
119 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
120 		return (EHOSTUNREACH);
121 	}
122 	/*
123 	 * Drop blackholed traffic and directed broadcasts.
124 	 */
125 	if ((pnh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) != 0) {
126 		IPSTAT_INC(ips_cantforward);
127 		m_freem(m);
128 		return (EHOSTUNREACH);
129 	}
130 
131 	if (pnh->nh_flags & NHF_REJECT) {
132 		IPSTAT_INC(ips_cantforward);
133 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
134 		return (EHOSTUNREACH);
135 	}
136 
137 	return (0);
138 }
139 
140 /*
141  * Try to forward a packet based on the destination address.
142  * This is a fast path optimized for the plain forwarding case.
143  * If the packet is handled (and consumed) here then we return NULL;
144  * otherwise mbuf is returned and the packet should be delivered
145  * to ip_input for full processing.
146  */
147 struct mbuf *
148 ip_tryforward(struct mbuf *m)
149 {
150 	struct ip *ip;
151 	struct mbuf *m0 = NULL;
152 	struct nhop4_basic nh;
153 	struct sockaddr_in dst;
154 	struct in_addr odest, dest;
155 	uint16_t ip_len, ip_off;
156 	int error = 0;
157 	struct m_tag *fwd_tag = NULL;
158 
159 	/*
160 	 * Are we active and forwarding packets?
161 	 */
162 
163 	M_ASSERTVALID(m);
164 	M_ASSERTPKTHDR(m);
165 
166 #ifdef ALTQ
167 	/*
168 	 * Is packet dropped by traffic conditioner?
169 	 */
170 	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
171 		goto drop;
172 #endif
173 
174 	/*
175 	 * Only IP packets without options
176 	 */
177 	ip = mtod(m, struct ip *);
178 
179 	if (ip->ip_hl != (sizeof(struct ip) >> 2)) {
180 		if (V_ip_doopts == 1)
181 			return m;
182 		else if (V_ip_doopts == 2) {
183 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,
184 				0, 0);
185 			return NULL;	/* mbuf already free'd */
186 		}
187 		/* else ignore IP options and continue */
188 	}
189 
190 	/*
191 	 * Only unicast IP, not from loopback, no L2 or IP broadcast,
192 	 * no multicast, no INADDR_ANY
193 	 *
194 	 * XXX: Probably some of these checks could be direct drop
195 	 * conditions.  However it is not clear whether there are some
196 	 * hacks or obscure behaviours which make it necessary to
197 	 * let ip_input handle it.  We play safe here and let ip_input
198 	 * deal with it until it is proven that we can directly drop it.
199 	 */
200 	if ((m->m_flags & (M_BCAST|M_MCAST)) ||
201 	    (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
202 	    ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
203 	    ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
204 	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
205 	    IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
206 	    IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) ||
207 	    IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
208 	    ip->ip_src.s_addr == INADDR_ANY ||
209 	    ip->ip_dst.s_addr == INADDR_ANY )
210 		return m;
211 
212 	/*
213 	 * Is it for a local address on this host?
214 	 */
215 	if (in_localip(ip->ip_dst))
216 		return m;
217 
218 	IPSTAT_INC(ips_total);
219 
220 	/*
221 	 * Step 3: incoming packet firewall processing
222 	 */
223 
224 	odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;
225 
226 	/*
227 	 * Run through list of ipfilter hooks for input packets
228 	 */
229 	if (!PFIL_HOOKED(&V_inet_pfil_hook))
230 		goto passin;
231 
232 	if (pfil_run_hooks(
233 	    &V_inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL) ||
234 	    m == NULL)
235 		goto drop;
236 
237 	M_ASSERTVALID(m);
238 	M_ASSERTPKTHDR(m);
239 
240 	ip = mtod(m, struct ip *);	/* m may have changed by pfil hook */
241 	dest.s_addr = ip->ip_dst.s_addr;
242 
243 	/*
244 	 * Destination address changed?
245 	 */
246 	if (odest.s_addr != dest.s_addr) {
247 		/*
248 		 * Is it now for a local address on this host?
249 		 */
250 		if (in_localip(dest))
251 			goto forwardlocal;
252 		/*
253 		 * Go on with new destination address
254 		 */
255 	}
256 
257 	if (m->m_flags & M_FASTFWD_OURS) {
258 		/*
259 		 * ipfw changed it for a local address on this host.
260 		 */
261 		goto forwardlocal;
262 	}
263 
264 passin:
265 	/*
266 	 * Step 4: decrement TTL and look up route
267 	 */
268 
269 	/*
270 	 * Check TTL
271 	 */
272 #ifdef IPSTEALTH
273 	if (!V_ipstealth) {
274 #endif
275 	if (ip->ip_ttl <= IPTTLDEC) {
276 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
277 		return NULL;	/* mbuf already free'd */
278 	}
279 
280 	/*
281 	 * Decrement the TTL and incrementally change the IP header checksum.
282 	 * Don't bother doing this with hw checksum offloading, it's faster
283 	 * doing it right here.
284 	 */
285 	ip->ip_ttl -= IPTTLDEC;
286 	if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
287 		ip->ip_sum -= ~htons(IPTTLDEC << 8);
288 	else
289 		ip->ip_sum += htons(IPTTLDEC << 8);
290 #ifdef IPSTEALTH
291 	}
292 #endif
293 
294 	/*
295 	 * Find route to destination.
296 	 */
297 	if (ip_findroute(&nh, dest, m) != 0)
298 		return (NULL);	/* icmp unreach already sent */
299 
300 	/*
301 	 * Step 5: outgoing firewall packet processing
302 	 */
303 	if (!PFIL_HOOKED(&V_inet_pfil_hook))
304 		goto passout;
305 
306 	if (pfil_run_hooks(&V_inet_pfil_hook, &m, nh.nh_ifp, PFIL_OUT, NULL) ||
307 	    m == NULL) {
308 		goto drop;
309 	}
310 
311 	M_ASSERTVALID(m);
312 	M_ASSERTPKTHDR(m);
313 
314 	ip = mtod(m, struct ip *);
315 	dest.s_addr = ip->ip_dst.s_addr;
316 
317 	/*
318 	 * Destination address changed?
319 	 */
320 	if (m->m_flags & M_IP_NEXTHOP)
321 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
322 	if (odest.s_addr != dest.s_addr || fwd_tag != NULL) {
323 		/*
324 		 * Is it now for a local address on this host?
325 		 */
326 		if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) {
327 forwardlocal:
328 			/*
329 			 * Return packet for processing by ip_input().
330 			 */
331 			m->m_flags |= M_FASTFWD_OURS;
332 			return (m);
333 		}
334 		/*
335 		 * Redo route lookup with new destination address
336 		 */
337 		if (fwd_tag) {
338 			dest.s_addr = ((struct sockaddr_in *)
339 				    (fwd_tag + 1))->sin_addr.s_addr;
340 			m_tag_delete(m, fwd_tag);
341 			m->m_flags &= ~M_IP_NEXTHOP;
342 		}
343 		if (ip_findroute(&nh, dest, m) != 0)
344 			return (NULL);	/* icmp unreach already sent */
345 	}
346 
347 passout:
348 	/*
349 	 * Step 6: send off the packet
350 	 */
351 	ip_len = ntohs(ip->ip_len);
352 	ip_off = ntohs(ip->ip_off);
353 
354 	bzero(&dst, sizeof(dst));
355 	dst.sin_family = AF_INET;
356 	dst.sin_len = sizeof(dst);
357 	dst.sin_addr = nh.nh_addr;
358 
359 	/*
360 	 * Check if packet fits MTU or if hardware will fragment for us
361 	 */
362 	if (ip_len <= nh.nh_mtu) {
363 		/*
364 		 * Avoid confusing lower layers.
365 		 */
366 		m_clrprotoflags(m);
367 		/*
368 		 * Send off the packet via outgoing interface
369 		 */
370 		IP_PROBE(send, NULL, NULL, ip, nh.nh_ifp, ip, NULL);
371 		error = (*nh.nh_ifp->if_output)(nh.nh_ifp, m,
372 		    (struct sockaddr *)&dst, NULL);
373 	} else {
374 		/*
375 		 * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery
376 		 */
377 		if (ip_off & IP_DF) {
378 			IPSTAT_INC(ips_cantfrag);
379 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
380 				0, nh.nh_mtu);
381 			goto consumed;
382 		} else {
383 			/*
384 			 * We have to fragment the packet
385 			 */
386 			m->m_pkthdr.csum_flags |= CSUM_IP;
387 			if (ip_fragment(ip, &m, nh.nh_mtu,
388 			    nh.nh_ifp->if_hwassist) != 0)
389 				goto drop;
390 			KASSERT(m != NULL, ("null mbuf and no error"));
391 			/*
392 			 * Send off the fragments via outgoing interface
393 			 */
394 			error = 0;
395 			do {
396 				m0 = m->m_nextpkt;
397 				m->m_nextpkt = NULL;
398 				/*
399 				 * Avoid confusing lower layers.
400 				 */
401 				m_clrprotoflags(m);
402 
403 				IP_PROBE(send, NULL, NULL,
404 				    mtod(m, struct ip *), nh.nh_ifp,
405 				    mtod(m, struct ip *), NULL);
406 				/* XXX: we can use cached route here */
407 				error = (*nh.nh_ifp->if_output)(nh.nh_ifp, m,
408 				    (struct sockaddr *)&dst, NULL);
409 				if (error)
410 					break;
411 			} while ((m = m0) != NULL);
412 			if (error) {
413 				/* Reclaim remaining fragments */
414 				for (m = m0; m; m = m0) {
415 					m0 = m->m_nextpkt;
416 					m_freem(m);
417 				}
418 			} else
419 				IPSTAT_INC(ips_fragmented);
420 		}
421 	}
422 
423 	if (error != 0)
424 		IPSTAT_INC(ips_odropped);
425 	else {
426 		IPSTAT_INC(ips_forward);
427 		IPSTAT_INC(ips_fastforward);
428 	}
429 consumed:
430 	return NULL;
431 drop:
432 	if (m)
433 		m_freem(m);
434 	return NULL;
435 }
436