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