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