]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/ip_fastfwd.c
kldstat(8): Fix indentation, whitespace to tabs.
[FreeBSD/FreeBSD.git] / sys / netinet / ip_fastfwd.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2003 Andre Oppermann, Internet Business Solutions AG
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
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
65 /*
66  * Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which
67  * is being followed here.
68  */
69
70 #include <sys/cdefs.h>
71 __FBSDID("$FreeBSD$");
72
73 #include "opt_ipstealth.h"
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/malloc.h>
79 #include <sys/mbuf.h>
80 #include <sys/protosw.h>
81 #include <sys/sdt.h>
82 #include <sys/socket.h>
83 #include <sys/sysctl.h>
84
85 #include <net/if.h>
86 #include <net/if_types.h>
87 #include <net/if_var.h>
88 #include <net/if_dl.h>
89 #include <net/pfil.h>
90 #include <net/route.h>
91 #include <net/route/nhop.h>
92 #include <net/vnet.h>
93
94 #include <netinet/in.h>
95 #include <netinet/in_fib.h>
96 #include <netinet/in_kdtrace.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/in_var.h>
99 #include <netinet/ip.h>
100 #include <netinet/ip_var.h>
101 #include <netinet/ip_icmp.h>
102 #include <netinet/ip_options.h>
103
104 #include <machine/in_cksum.h>
105
106 #define V_ipsendredirects       VNET(ipsendredirects)
107
108 static struct mbuf *
109 ip_redir_alloc(struct mbuf *m, struct nhop_object *nh, u_short ip_len,
110     struct in_addr *osrc, struct in_addr *newgw)
111 {
112         struct in_ifaddr *nh_ia;
113         struct mbuf *mcopy;
114
115         KASSERT(nh != NULL, ("%s: m %p nh is NULL\n", __func__, m));
116
117         /*
118          * Only send a redirect if:
119          * - Redirects are not disabled (must be checked by caller),
120          * - We have not applied NAT (must be checked by caller as possible),
121          * - Neither a MCAST or BCAST packet (must be checked by caller)
122          *   [RFC1009 Appendix A.2].
123          * - The packet does not do IP source routing or having any other
124          *   IP options (this case was handled already by ip_input() calling
125          *   ip_dooptions() [RFC792, p13],
126          * - The packet is being forwarded out the same physical interface
127          *   that it was received from [RFC1812, 5.2.7.2].
128          */
129
130         /*
131          * - The forwarding route was not created by a redirect
132          *   [RFC1812, 5.2.7.2], or
133          *   if it was to follow a default route (see below).
134          * - The next-hop is reachable by us [RFC1009 Appendix A.2].
135          */
136         if ((nh->nh_flags & (NHF_DEFAULT | NHF_REDIRECT |
137             NHF_BLACKHOLE | NHF_REJECT)) != 0)
138                 return (NULL);
139
140         /* Get the new gateway. */
141         if ((nh->nh_flags & NHF_GATEWAY) == 0 || nh->gw_sa.sa_family != AF_INET)
142                 return (NULL);
143         newgw->s_addr = nh->gw4_sa.sin_addr.s_addr;
144
145         /*
146          * - The resulting forwarding destination is not "This host on this
147          *   network" [RFC1122, Section 3.2.1.3] (default route check above).
148          */
149         if (newgw->s_addr == 0)
150                 return (NULL);
151
152         /*
153          * - We know how to reach the sender and the source address is
154          *   directly connected to us [RFC792, p13].
155          * + The new gateway address and the source address are on the same
156          *   subnet [RFC1009 Appendix A.2, RFC1122 3.2.2.2, RFC1812, 5.2.7.2].
157          * NB: if you think multiple logical subnets on the same wire should
158          *     receive redirects read [RFC1812, APPENDIX C (14->15)].
159          */
160         nh_ia = (struct in_ifaddr *)nh->nh_ifa;
161         if ((ntohl(osrc->s_addr) & nh_ia->ia_subnetmask) != nh_ia->ia_subnet)
162                 return (NULL);
163
164         /* Prepare for sending the redirect. */
165
166         /*
167          * Make a copy of as much as we need of the packet as the original
168          * one will be forwarded but we need (a portion) for icmp_error().
169          */
170         mcopy = m_gethdr(M_NOWAIT, m->m_type);
171         if (mcopy == NULL)
172                 return (NULL);
173
174         if (m_dup_pkthdr(mcopy, m, M_NOWAIT) == 0) {
175                 /*
176                  * It's probably ok if the pkthdr dup fails (because
177                  * the deep copy of the tag chain failed), but for now
178                  * be conservative and just discard the copy since
179                  * code below may some day want the tags.
180                  */
181                 m_free(mcopy);
182                 return (NULL);
183         }
184         mcopy->m_len = min(ip_len, M_TRAILINGSPACE(mcopy));
185         mcopy->m_pkthdr.len = mcopy->m_len;
186         m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
187
188         return (mcopy);
189 }
190
191
192 static int
193 ip_findroute(struct nhop_object **pnh, struct in_addr dest, struct mbuf *m)
194 {
195         struct nhop_object *nh;
196
197         nh = fib4_lookup(M_GETFIB(m), dest, 0, NHR_NONE,
198             m->m_pkthdr.flowid);
199         if (nh == NULL) {
200                 IPSTAT_INC(ips_noroute);
201                 IPSTAT_INC(ips_cantforward);
202                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
203                 return (EHOSTUNREACH);
204         }
205         /*
206          * Drop blackholed traffic and directed broadcasts.
207          */
208         if ((nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) != 0) {
209                 IPSTAT_INC(ips_cantforward);
210                 m_freem(m);
211                 return (EHOSTUNREACH);
212         }
213
214         if (nh->nh_flags & NHF_REJECT) {
215                 IPSTAT_INC(ips_cantforward);
216                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
217                 return (EHOSTUNREACH);
218         }
219
220         *pnh = nh;
221
222         return (0);
223 }
224
225 /*
226  * Try to forward a packet based on the destination address.
227  * This is a fast path optimized for the plain forwarding case.
228  * If the packet is handled (and consumed) here then we return NULL;
229  * otherwise mbuf is returned and the packet should be delivered
230  * to ip_input for full processing.
231  */
232 struct mbuf *
233 ip_tryforward(struct mbuf *m)
234 {
235         struct ip *ip;
236         struct mbuf *m0 = NULL;
237         struct nhop_object *nh = NULL;
238         struct route ro;
239         struct sockaddr_in *dst;
240         const struct sockaddr *gw;
241         struct in_addr dest, odest, rtdest, osrc;
242         uint16_t ip_len, ip_off;
243         int error = 0;
244         struct m_tag *fwd_tag = NULL;
245         struct mbuf *mcopy = NULL;
246         struct in_addr redest;
247         /*
248          * Are we active and forwarding packets?
249          */
250
251         M_ASSERTVALID(m);
252         M_ASSERTPKTHDR(m);
253
254 #ifdef ALTQ
255         /*
256          * Is packet dropped by traffic conditioner?
257          */
258         if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
259                 goto drop;
260 #endif
261
262         /*
263          * Only IP packets without options
264          */
265         ip = mtod(m, struct ip *);
266
267         if (ip->ip_hl != (sizeof(struct ip) >> 2)) {
268                 if (V_ip_doopts == 1)
269                         return m;
270                 else if (V_ip_doopts == 2) {
271                         icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,
272                                 0, 0);
273                         return NULL;    /* mbuf already free'd */
274                 }
275                 /* else ignore IP options and continue */
276         }
277
278         /*
279          * Only unicast IP, not from loopback, no L2 or IP broadcast,
280          * no multicast, no INADDR_ANY
281          *
282          * XXX: Probably some of these checks could be direct drop
283          * conditions.  However it is not clear whether there are some
284          * hacks or obscure behaviours which make it necessary to
285          * let ip_input handle it.  We play safe here and let ip_input
286          * deal with it until it is proven that we can directly drop it.
287          */
288         if ((m->m_flags & (M_BCAST|M_MCAST)) ||
289             (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
290             ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
291             ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
292             IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
293             IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
294             IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) ||
295             IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
296             ip->ip_src.s_addr == INADDR_ANY ||
297             ip->ip_dst.s_addr == INADDR_ANY )
298                 return m;
299
300         /*
301          * Is it for a local address on this host?
302          */
303         if (in_localip(ip->ip_dst))
304                 return m;
305
306         IPSTAT_INC(ips_total);
307
308         /*
309          * Step 3: incoming packet firewall processing
310          */
311
312         odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;
313         osrc.s_addr = ip->ip_src.s_addr;
314
315         /*
316          * Run through list of ipfilter hooks for input packets
317          */
318         if (!PFIL_HOOKED_IN(V_inet_pfil_head))
319                 goto passin;
320
321         if (pfil_run_hooks(V_inet_pfil_head, &m, m->m_pkthdr.rcvif, PFIL_IN,
322             NULL) != PFIL_PASS)
323                 goto drop;
324
325         M_ASSERTVALID(m);
326         M_ASSERTPKTHDR(m);
327
328         ip = mtod(m, struct ip *);      /* m may have changed by pfil hook */
329         dest.s_addr = ip->ip_dst.s_addr;
330
331         /*
332          * Destination address changed?
333          */
334         if (odest.s_addr != dest.s_addr) {
335                 /*
336                  * Is it now for a local address on this host?
337                  */
338                 if (in_localip(dest))
339                         goto forwardlocal;
340                 /*
341                  * Go on with new destination address
342                  */
343         }
344
345         if (m->m_flags & M_FASTFWD_OURS) {
346                 /*
347                  * ipfw changed it for a local address on this host.
348                  */
349                 goto forwardlocal;
350         }
351
352 passin:
353         /*
354          * Step 4: decrement TTL and look up route
355          */
356
357         /*
358          * Check TTL
359          */
360 #ifdef IPSTEALTH
361         if (!V_ipstealth) {
362 #endif
363         if (ip->ip_ttl <= IPTTLDEC) {
364                 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
365                 return NULL;    /* mbuf already free'd */
366         }
367
368         /*
369          * Decrement the TTL and incrementally change the IP header checksum.
370          * Don't bother doing this with hw checksum offloading, it's faster
371          * doing it right here.
372          */
373         ip->ip_ttl -= IPTTLDEC;
374         if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
375                 ip->ip_sum -= ~htons(IPTTLDEC << 8);
376         else
377                 ip->ip_sum += htons(IPTTLDEC << 8);
378 #ifdef IPSTEALTH
379         }
380 #endif
381
382         /*
383          * Next hop forced by pfil(9) hook?
384          */
385         if ((m->m_flags & M_IP_NEXTHOP) &&
386             ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {
387                 /*
388                  * Now we will find route to forced destination.
389                  */
390                 dest.s_addr = ((struct sockaddr_in *)
391                             (fwd_tag + 1))->sin_addr.s_addr;
392                 m_tag_delete(m, fwd_tag);
393                 m->m_flags &= ~M_IP_NEXTHOP;
394         }
395
396         /*
397          * Find route to destination.
398          */
399         if (ip_findroute(&nh, dest, m) != 0)
400                 return (NULL);  /* icmp unreach already sent */
401
402         /*
403          * Avoid second route lookup by caching destination.
404          */
405         rtdest.s_addr = dest.s_addr;
406
407         /*
408          * Step 5: outgoing firewall packet processing
409          */
410         if (!PFIL_HOOKED_OUT(V_inet_pfil_head))
411                 goto passout;
412
413         if (pfil_run_hooks(V_inet_pfil_head, &m, nh->nh_ifp,
414             PFIL_OUT | PFIL_FWD, NULL) != PFIL_PASS)
415                 goto drop;
416
417         M_ASSERTVALID(m);
418         M_ASSERTPKTHDR(m);
419
420         ip = mtod(m, struct ip *);
421         dest.s_addr = ip->ip_dst.s_addr;
422
423         /*
424          * Destination address changed?
425          */
426         if (m->m_flags & M_IP_NEXTHOP)
427                 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
428         else
429                 fwd_tag = NULL;
430         if (odest.s_addr != dest.s_addr || fwd_tag != NULL) {
431                 /*
432                  * Is it now for a local address on this host?
433                  */
434                 if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) {
435 forwardlocal:
436                         /*
437                          * Return packet for processing by ip_input().
438                          */
439                         m->m_flags |= M_FASTFWD_OURS;
440                         return (m);
441                 }
442                 /*
443                  * Redo route lookup with new destination address
444                  */
445                 if (fwd_tag) {
446                         dest.s_addr = ((struct sockaddr_in *)
447                                     (fwd_tag + 1))->sin_addr.s_addr;
448                         m_tag_delete(m, fwd_tag);
449                         m->m_flags &= ~M_IP_NEXTHOP;
450                 }
451                 if (dest.s_addr != rtdest.s_addr &&
452                     ip_findroute(&nh, dest, m) != 0)
453                         return (NULL);  /* icmp unreach already sent */
454         }
455
456 passout:
457         /*
458          * Step 6: send off the packet
459          */
460         ip_len = ntohs(ip->ip_len);
461         ip_off = ntohs(ip->ip_off);
462
463         bzero(&ro, sizeof(ro));
464         dst = (struct sockaddr_in *)&ro.ro_dst;
465         dst->sin_family = AF_INET;
466         dst->sin_len = sizeof(*dst);
467         dst->sin_addr = dest;
468         if (nh->nh_flags & NHF_GATEWAY) {
469                 gw = &nh->gw_sa;
470                 ro.ro_flags |= RT_HAS_GW;
471         } else
472                 gw = (const struct sockaddr *)dst;
473
474         /* Handle redirect case. */
475         redest.s_addr = 0;
476         if (V_ipsendredirects && osrc.s_addr == ip->ip_src.s_addr &&
477             nh->nh_ifp == m->m_pkthdr.rcvif)
478                 mcopy = ip_redir_alloc(m, nh, ip_len, &osrc, &redest);
479
480         /*
481          * Check if packet fits MTU or if hardware will fragment for us
482          */
483         if (ip_len <= nh->nh_mtu) {
484                 /*
485                  * Avoid confusing lower layers.
486                  */
487                 m_clrprotoflags(m);
488                 /*
489                  * Send off the packet via outgoing interface
490                  */
491                 IP_PROBE(send, NULL, NULL, ip, nh->nh_ifp, ip, NULL);
492                 error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m, gw, &ro);
493         } else {
494                 /*
495                  * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery
496                  */
497                 if (ip_off & IP_DF) {
498                         IPSTAT_INC(ips_cantfrag);
499                         icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
500                                 0, nh->nh_mtu);
501                         goto consumed;
502                 } else {
503                         /*
504                          * We have to fragment the packet
505                          */
506                         m->m_pkthdr.csum_flags |= CSUM_IP;
507                         if (ip_fragment(ip, &m, nh->nh_mtu,
508                             nh->nh_ifp->if_hwassist) != 0)
509                                 goto drop;
510                         KASSERT(m != NULL, ("null mbuf and no error"));
511                         /*
512                          * Send off the fragments via outgoing interface
513                          */
514                         error = 0;
515                         do {
516                                 m0 = m->m_nextpkt;
517                                 m->m_nextpkt = NULL;
518                                 /*
519                                  * Avoid confusing lower layers.
520                                  */
521                                 m_clrprotoflags(m);
522
523                                 IP_PROBE(send, NULL, NULL,
524                                     mtod(m, struct ip *), nh->nh_ifp,
525                                     mtod(m, struct ip *), NULL);
526                                 error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m,
527                                     gw, &ro);
528                                 if (error)
529                                         break;
530                         } while ((m = m0) != NULL);
531                         if (error) {
532                                 /* Reclaim remaining fragments */
533                                 for (m = m0; m; m = m0) {
534                                         m0 = m->m_nextpkt;
535                                         m_freem(m);
536                                 }
537                         } else
538                                 IPSTAT_INC(ips_fragmented);
539                 }
540         }
541
542         if (error != 0)
543                 IPSTAT_INC(ips_odropped);
544         else {
545                 IPSTAT_INC(ips_forward);
546                 IPSTAT_INC(ips_fastforward);
547         }
548
549         /* Send required redirect */
550         if (mcopy != NULL) {
551                 icmp_error(mcopy, ICMP_REDIRECT, ICMP_REDIRECT_HOST, redest.s_addr, 0);
552                 mcopy = NULL; /* Was consumed by callee. */
553         }
554
555 consumed:
556         if (mcopy != NULL)
557                 m_freem(mcopy);
558         return NULL;
559 drop:
560         if (m)
561                 m_freem(m);
562         return NULL;
563 }