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