]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/ip_fastfwd.c
An earlier commit effectively turned out the fast forwading path
[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  * We don't do ICMP redirect in the fast forwarding path. I have had my own
65  * cases where two core routers with Zebra routing suite would send millions
66  * ICMP redirects to connected hosts if the destination router was not the
67  * default gateway. In one case it was filling the routing table of a host
68  * with approximately 300.000 cloned redirect entries until it ran out of
69  * kernel memory. However the networking code proved very robust and it didn't
70  * crash or fail in other ways.
71  */
72
73 /*
74  * Many thanks to Matt Thomas of NetBSD for basic structure of ip_flow.c which
75  * is being followed here.
76  */
77
78 #include <sys/cdefs.h>
79 __FBSDID("$FreeBSD$");
80
81 #include "opt_ipstealth.h"
82
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
86 #include <sys/malloc.h>
87 #include <sys/mbuf.h>
88 #include <sys/protosw.h>
89 #include <sys/sdt.h>
90 #include <sys/socket.h>
91 #include <sys/sysctl.h>
92
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/pfil.h>
98 #include <net/route.h>
99 #include <net/route/nhop.h>
100 #include <net/vnet.h>
101
102 #include <netinet/in.h>
103 #include <netinet/in_fib.h>
104 #include <netinet/in_kdtrace.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/in_var.h>
107 #include <netinet/ip.h>
108 #include <netinet/ip_var.h>
109 #include <netinet/ip_icmp.h>
110 #include <netinet/ip_options.h>
111
112 #include <machine/in_cksum.h>
113
114 #define V_ipsendredirects       VNET(ipsendredirects)
115
116 static struct mbuf *
117 ip_redir_alloc(struct mbuf *m, struct nhop_object *nh,
118     struct ip *ip, in_addr_t *addr)
119 {
120         struct mbuf *mcopy = m_gethdr(M_NOWAIT, m->m_type);
121         if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
122                 /*
123                  * It's probably ok if the pkthdr dup fails (because
124                  * the deep copy of the tag chain failed), but for now
125                  * be conservative and just discard the copy since
126                  * code below may some day want the tags.
127                  */
128                 m_free(mcopy);
129                 return (NULL);
130         } 
131         mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy));
132         mcopy->m_pkthdr.len = mcopy->m_len;
133         m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
134         
135         if (nh != NULL &&
136             ((nh->nh_flags & (NHF_REDIRECT|NHF_DEFAULT)) == 0)) {
137                 struct in_ifaddr *nh_ia = (struct in_ifaddr *)(nh->nh_ifa);
138                 u_long src = ntohl(ip->ip_src.s_addr);
139                 
140                 if (nh_ia != NULL && (src & nh_ia->ia_subnetmask) == nh_ia->ia_subnet) {
141                         if (nh->nh_flags & NHF_GATEWAY)
142                                 *addr = nh->gw4_sa.sin_addr.s_addr;
143                         else
144                                 *addr = ip->ip_dst.s_addr;
145                 }
146         }
147         return (mcopy);
148 }
149
150
151 static int
152 ip_findroute(struct nhop_object **pnh, struct in_addr dest, struct mbuf *m)
153 {
154         struct nhop_object *nh;
155
156         nh = fib4_lookup(M_GETFIB(m), dest, 0, NHR_NONE,
157             m->m_pkthdr.flowid);
158         if (nh == NULL) {
159                 IPSTAT_INC(ips_noroute);
160                 IPSTAT_INC(ips_cantforward);
161                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
162                 return (EHOSTUNREACH);
163         }
164         /*
165          * Drop blackholed traffic and directed broadcasts.
166          */
167         if ((nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) != 0) {
168                 IPSTAT_INC(ips_cantforward);
169                 m_freem(m);
170                 return (EHOSTUNREACH);
171         }
172
173         if (nh->nh_flags & NHF_REJECT) {
174                 IPSTAT_INC(ips_cantforward);
175                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
176                 return (EHOSTUNREACH);
177         }
178
179         *pnh = nh;
180
181         return (0);
182 }
183
184 /*
185  * Try to forward a packet based on the destination address.
186  * This is a fast path optimized for the plain forwarding case.
187  * If the packet is handled (and consumed) here then we return NULL;
188  * otherwise mbuf is returned and the packet should be delivered
189  * to ip_input for full processing.
190  */
191 struct mbuf *
192 ip_tryforward(struct mbuf *m)
193 {
194         struct ip *ip;
195         struct mbuf *m0 = NULL;
196         struct nhop_object *nh = NULL;
197         struct sockaddr_in dst;
198         struct in_addr dest, odest, rtdest;
199         uint16_t ip_len, ip_off;
200         int error = 0;
201         struct m_tag *fwd_tag = NULL;
202         struct mbuf *mcopy = NULL;
203         struct in_addr redest;
204         /*
205          * Are we active and forwarding packets?
206          */
207
208         M_ASSERTVALID(m);
209         M_ASSERTPKTHDR(m);
210
211 #ifdef ALTQ
212         /*
213          * Is packet dropped by traffic conditioner?
214          */
215         if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
216                 goto drop;
217 #endif
218
219         /*
220          * Only IP packets without options
221          */
222         ip = mtod(m, struct ip *);
223
224         if (ip->ip_hl != (sizeof(struct ip) >> 2)) {
225                 if (V_ip_doopts == 1)
226                         return m;
227                 else if (V_ip_doopts == 2) {
228                         icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB,
229                                 0, 0);
230                         return NULL;    /* mbuf already free'd */
231                 }
232                 /* else ignore IP options and continue */
233         }
234
235         /*
236          * Only unicast IP, not from loopback, no L2 or IP broadcast,
237          * no multicast, no INADDR_ANY
238          *
239          * XXX: Probably some of these checks could be direct drop
240          * conditions.  However it is not clear whether there are some
241          * hacks or obscure behaviours which make it necessary to
242          * let ip_input handle it.  We play safe here and let ip_input
243          * deal with it until it is proven that we can directly drop it.
244          */
245         if ((m->m_flags & (M_BCAST|M_MCAST)) ||
246             (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) ||
247             ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST ||
248             ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST ||
249             IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
250             IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
251             IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) ||
252             IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
253             ip->ip_src.s_addr == INADDR_ANY ||
254             ip->ip_dst.s_addr == INADDR_ANY )
255                 return m;
256
257         /*
258          * Is it for a local address on this host?
259          */
260         if (in_localip(ip->ip_dst))
261                 return m;
262
263         IPSTAT_INC(ips_total);
264
265         /*
266          * Step 3: incoming packet firewall processing
267          */
268
269         odest.s_addr = dest.s_addr = ip->ip_dst.s_addr;
270
271         /*
272          * Run through list of ipfilter hooks for input packets
273          */
274         if (!PFIL_HOOKED_IN(V_inet_pfil_head))
275                 goto passin;
276
277         if (pfil_run_hooks(V_inet_pfil_head, &m, m->m_pkthdr.rcvif, PFIL_IN,
278             NULL) != PFIL_PASS)
279                 goto drop;
280
281         M_ASSERTVALID(m);
282         M_ASSERTPKTHDR(m);
283
284         ip = mtod(m, struct ip *);      /* m may have changed by pfil hook */
285         dest.s_addr = ip->ip_dst.s_addr;
286
287         /*
288          * Destination address changed?
289          */
290         if (odest.s_addr != dest.s_addr) {
291                 /*
292                  * Is it now for a local address on this host?
293                  */
294                 if (in_localip(dest))
295                         goto forwardlocal;
296                 /*
297                  * Go on with new destination address
298                  */
299         }
300
301         if (m->m_flags & M_FASTFWD_OURS) {
302                 /*
303                  * ipfw changed it for a local address on this host.
304                  */
305                 goto forwardlocal;
306         }
307
308 passin:
309         /*
310          * Step 4: decrement TTL and look up route
311          */
312
313         /*
314          * Check TTL
315          */
316 #ifdef IPSTEALTH
317         if (!V_ipstealth) {
318 #endif
319         if (ip->ip_ttl <= IPTTLDEC) {
320                 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
321                 return NULL;    /* mbuf already free'd */
322         }
323
324         /*
325          * Decrement the TTL and incrementally change the IP header checksum.
326          * Don't bother doing this with hw checksum offloading, it's faster
327          * doing it right here.
328          */
329         ip->ip_ttl -= IPTTLDEC;
330         if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
331                 ip->ip_sum -= ~htons(IPTTLDEC << 8);
332         else
333                 ip->ip_sum += htons(IPTTLDEC << 8);
334 #ifdef IPSTEALTH
335         }
336 #endif
337
338         /*
339          * Next hop forced by pfil(9) hook?
340          */
341         if ((m->m_flags & M_IP_NEXTHOP) &&
342             ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {
343                 /*
344                  * Now we will find route to forced destination.
345                  */
346                 dest.s_addr = ((struct sockaddr_in *)
347                             (fwd_tag + 1))->sin_addr.s_addr;
348                 m_tag_delete(m, fwd_tag);
349                 m->m_flags &= ~M_IP_NEXTHOP;
350         }
351
352         /*
353          * Find route to destination.
354          */
355         if (ip_findroute(&nh, dest, m) != 0)
356                 return (NULL);  /* icmp unreach already sent */
357
358         /*
359          * Avoid second route lookup by caching destination.
360          */
361         rtdest.s_addr = dest.s_addr;
362
363         /*
364          * Step 5: outgoing firewall packet processing
365          */
366         if (!PFIL_HOOKED_OUT(V_inet_pfil_head))
367                 goto passout;
368
369         if (pfil_run_hooks(V_inet_pfil_head, &m, nh->nh_ifp,
370             PFIL_OUT | PFIL_FWD, NULL) != PFIL_PASS)
371                 goto drop;
372
373         M_ASSERTVALID(m);
374         M_ASSERTPKTHDR(m);
375
376         ip = mtod(m, struct ip *);
377         dest.s_addr = ip->ip_dst.s_addr;
378
379         /*
380          * Destination address changed?
381          */
382         if (m->m_flags & M_IP_NEXTHOP)
383                 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
384         else
385                 fwd_tag = NULL;
386         if (odest.s_addr != dest.s_addr || fwd_tag != NULL) {
387                 /*
388                  * Is it now for a local address on this host?
389                  */
390                 if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) {
391 forwardlocal:
392                         /*
393                          * Return packet for processing by ip_input().
394                          */
395                         m->m_flags |= M_FASTFWD_OURS;
396                         return (m);
397                 }
398                 /*
399                  * Redo route lookup with new destination address
400                  */
401                 if (fwd_tag) {
402                         dest.s_addr = ((struct sockaddr_in *)
403                                     (fwd_tag + 1))->sin_addr.s_addr;
404                         m_tag_delete(m, fwd_tag);
405                         m->m_flags &= ~M_IP_NEXTHOP;
406                 }
407                 if (dest.s_addr != rtdest.s_addr &&
408                     ip_findroute(&nh, dest, m) != 0)
409                         return (NULL);  /* icmp unreach already sent */
410         }
411
412 passout:
413         /*
414          * Step 6: send off the packet
415          */
416         ip_len = ntohs(ip->ip_len);
417         ip_off = ntohs(ip->ip_off);
418
419         bzero(&dst, sizeof(dst));
420         dst.sin_family = AF_INET;
421         dst.sin_len = sizeof(dst);
422         if (nh->nh_flags & NHF_GATEWAY)
423                 dst.sin_addr = nh->gw4_sa.sin_addr;
424         else
425                 dst.sin_addr = dest;
426
427         /*
428          * Handle redirect case.
429          */
430         redest.s_addr = 0;
431         if (V_ipsendredirects && (nh->nh_ifp == m->m_pkthdr.rcvif))
432                 mcopy = ip_redir_alloc(m, nh, ip, &redest.s_addr);
433
434         /*
435          * Check if packet fits MTU or if hardware will fragment for us
436          */
437         if (ip_len <= nh->nh_mtu) {
438                 /*
439                  * Avoid confusing lower layers.
440                  */
441                 m_clrprotoflags(m);
442                 /*
443                  * Send off the packet via outgoing interface
444                  */
445                 IP_PROBE(send, NULL, NULL, ip, nh->nh_ifp, ip, NULL);
446                 error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m,
447                     (struct sockaddr *)&dst, NULL);
448         } else {
449                 /*
450                  * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery
451                  */
452                 if (ip_off & IP_DF) {
453                         IPSTAT_INC(ips_cantfrag);
454                         icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
455                                 0, nh->nh_mtu);
456                         goto consumed;
457                 } else {
458                         /*
459                          * We have to fragment the packet
460                          */
461                         m->m_pkthdr.csum_flags |= CSUM_IP;
462                         if (ip_fragment(ip, &m, nh->nh_mtu,
463                             nh->nh_ifp->if_hwassist) != 0)
464                                 goto drop;
465                         KASSERT(m != NULL, ("null mbuf and no error"));
466                         /*
467                          * Send off the fragments via outgoing interface
468                          */
469                         error = 0;
470                         do {
471                                 m0 = m->m_nextpkt;
472                                 m->m_nextpkt = NULL;
473                                 /*
474                                  * Avoid confusing lower layers.
475                                  */
476                                 m_clrprotoflags(m);
477
478                                 IP_PROBE(send, NULL, NULL,
479                                     mtod(m, struct ip *), nh->nh_ifp,
480                                     mtod(m, struct ip *), NULL);
481                                 error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m,
482                                     (struct sockaddr *)&dst, NULL);
483                                 if (error)
484                                         break;
485                         } while ((m = m0) != NULL);
486                         if (error) {
487                                 /* Reclaim remaining fragments */
488                                 for (m = m0; m; m = m0) {
489                                         m0 = m->m_nextpkt;
490                                         m_freem(m);
491                                 }
492                         } else
493                                 IPSTAT_INC(ips_fragmented);
494                 }
495         }
496
497         if (error != 0)
498                 IPSTAT_INC(ips_odropped);
499         else {
500                 IPSTAT_INC(ips_forward);
501                 IPSTAT_INC(ips_fastforward);
502         }
503
504         /* Send required redirect */
505         if (mcopy != NULL) {
506                 icmp_error(mcopy, ICMP_REDIRECT, ICMP_REDIRECT_HOST, redest.s_addr, 0);
507                 mcopy = NULL; /* Freed by caller */
508         }
509
510 consumed:
511         if (mcopy != NULL)
512                 m_freem(mcopy);
513         return NULL;
514 drop:
515         if (m)
516                 m_freem(m);
517         return NULL;
518 }