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