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 static int 115 ip_findroute(struct nhop_object **pnh, struct in_addr dest, struct mbuf *m) 116 { 117 struct nhop_object *nh; 118 119 nh = fib4_lookup(M_GETFIB(m), dest, 0, NHR_NONE, 120 m->m_pkthdr.flowid); 121 if (nh == NULL) { 122 IPSTAT_INC(ips_noroute); 123 IPSTAT_INC(ips_cantforward); 124 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 125 return (EHOSTUNREACH); 126 } 127 /* 128 * Drop blackholed traffic and directed broadcasts. 129 */ 130 if ((nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) != 0) { 131 IPSTAT_INC(ips_cantforward); 132 m_freem(m); 133 return (EHOSTUNREACH); 134 } 135 136 if (nh->nh_flags & NHF_REJECT) { 137 IPSTAT_INC(ips_cantforward); 138 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 139 return (EHOSTUNREACH); 140 } 141 142 *pnh = nh; 143 144 return (0); 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 NULL; 151 * otherwise mbuf is returned and the packet should be delivered 152 * to ip_input for full processing. 153 */ 154 struct mbuf * 155 ip_tryforward(struct mbuf *m) 156 { 157 struct ip *ip; 158 struct mbuf *m0 = NULL; 159 struct nhop_object *nh; 160 struct sockaddr_in dst; 161 struct in_addr dest, odest, rtdest; 162 uint16_t ip_len, ip_off; 163 int error = 0; 164 struct m_tag *fwd_tag = NULL; 165 166 /* 167 * Are we active and forwarding packets? 168 */ 169 170 M_ASSERTVALID(m); 171 M_ASSERTPKTHDR(m); 172 173 #ifdef ALTQ 174 /* 175 * Is packet dropped by traffic conditioner? 176 */ 177 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 178 goto drop; 179 #endif 180 181 /* 182 * Only IP packets without options 183 */ 184 ip = mtod(m, struct ip *); 185 186 if (ip->ip_hl != (sizeof(struct ip) >> 2)) { 187 if (V_ip_doopts == 1) 188 return m; 189 else if (V_ip_doopts == 2) { 190 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB, 191 0, 0); 192 return NULL; /* mbuf already free'd */ 193 } 194 /* else ignore IP options and continue */ 195 } 196 197 /* 198 * Only unicast IP, not from loopback, no L2 or IP broadcast, 199 * no multicast, no INADDR_ANY 200 * 201 * XXX: Probably some of these checks could be direct drop 202 * conditions. However it is not clear whether there are some 203 * hacks or obscure behaviours which make it necessary to 204 * let ip_input handle it. We play safe here and let ip_input 205 * deal with it until it is proven that we can directly drop it. 206 */ 207 if ((m->m_flags & (M_BCAST|M_MCAST)) || 208 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) || 209 ntohl(ip->ip_src.s_addr) == (u_long)INADDR_BROADCAST || 210 ntohl(ip->ip_dst.s_addr) == (u_long)INADDR_BROADCAST || 211 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 212 IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 213 IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) || 214 IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) || 215 ip->ip_src.s_addr == INADDR_ANY || 216 ip->ip_dst.s_addr == INADDR_ANY ) 217 return m; 218 219 /* 220 * Is it for a local address on this host? 221 */ 222 if (in_localip(ip->ip_dst)) 223 return m; 224 225 IPSTAT_INC(ips_total); 226 227 /* 228 * Step 3: incoming packet firewall processing 229 */ 230 231 odest.s_addr = dest.s_addr = ip->ip_dst.s_addr; 232 233 /* 234 * Run through list of ipfilter hooks for input packets 235 */ 236 if (!PFIL_HOOKED_IN(V_inet_pfil_head)) 237 goto passin; 238 239 if (pfil_run_hooks(V_inet_pfil_head, &m, m->m_pkthdr.rcvif, PFIL_IN, 240 NULL) != PFIL_PASS) 241 goto drop; 242 243 M_ASSERTVALID(m); 244 M_ASSERTPKTHDR(m); 245 246 ip = mtod(m, struct ip *); /* m may have changed by pfil hook */ 247 dest.s_addr = ip->ip_dst.s_addr; 248 249 /* 250 * Destination address changed? 251 */ 252 if (odest.s_addr != dest.s_addr) { 253 /* 254 * Is it now for a local address on this host? 255 */ 256 if (in_localip(dest)) 257 goto forwardlocal; 258 /* 259 * Go on with new destination address 260 */ 261 } 262 263 if (m->m_flags & M_FASTFWD_OURS) { 264 /* 265 * ipfw changed it for a local address on this host. 266 */ 267 goto forwardlocal; 268 } 269 270 passin: 271 /* 272 * Step 4: decrement TTL and look up route 273 */ 274 275 /* 276 * Check TTL 277 */ 278 #ifdef IPSTEALTH 279 if (!V_ipstealth) { 280 #endif 281 if (ip->ip_ttl <= IPTTLDEC) { 282 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0); 283 return NULL; /* mbuf already free'd */ 284 } 285 286 /* 287 * Decrement the TTL and incrementally change the IP header checksum. 288 * Don't bother doing this with hw checksum offloading, it's faster 289 * doing it right here. 290 */ 291 ip->ip_ttl -= IPTTLDEC; 292 if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8)) 293 ip->ip_sum -= ~htons(IPTTLDEC << 8); 294 else 295 ip->ip_sum += htons(IPTTLDEC << 8); 296 #ifdef IPSTEALTH 297 } 298 #endif 299 300 /* 301 * Next hop forced by pfil(9) hook? 302 */ 303 if ((m->m_flags & M_IP_NEXTHOP) && 304 ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) { 305 /* 306 * Now we will find route to forced destination. 307 */ 308 dest.s_addr = ((struct sockaddr_in *) 309 (fwd_tag + 1))->sin_addr.s_addr; 310 m_tag_delete(m, fwd_tag); 311 m->m_flags &= ~M_IP_NEXTHOP; 312 } 313 314 /* 315 * Find route to destination. 316 */ 317 if (ip_findroute(&nh, dest, m) != 0) 318 return (NULL); /* icmp unreach already sent */ 319 320 /* 321 * Avoid second route lookup by caching destination. 322 */ 323 rtdest.s_addr = dest.s_addr; 324 325 /* 326 * Step 5: outgoing firewall packet processing 327 */ 328 if (!PFIL_HOOKED_OUT(V_inet_pfil_head)) 329 goto passout; 330 331 if (pfil_run_hooks(V_inet_pfil_head, &m, nh->nh_ifp, 332 PFIL_OUT | PFIL_FWD, NULL) != PFIL_PASS) 333 goto drop; 334 335 M_ASSERTVALID(m); 336 M_ASSERTPKTHDR(m); 337 338 ip = mtod(m, struct ip *); 339 dest.s_addr = ip->ip_dst.s_addr; 340 341 /* 342 * Destination address changed? 343 */ 344 if (m->m_flags & M_IP_NEXTHOP) 345 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 346 else 347 fwd_tag = NULL; 348 if (odest.s_addr != dest.s_addr || fwd_tag != NULL) { 349 /* 350 * Is it now for a local address on this host? 351 */ 352 if (m->m_flags & M_FASTFWD_OURS || in_localip(dest)) { 353 forwardlocal: 354 /* 355 * Return packet for processing by ip_input(). 356 */ 357 m->m_flags |= M_FASTFWD_OURS; 358 return (m); 359 } 360 /* 361 * Redo route lookup with new destination address 362 */ 363 if (fwd_tag) { 364 dest.s_addr = ((struct sockaddr_in *) 365 (fwd_tag + 1))->sin_addr.s_addr; 366 m_tag_delete(m, fwd_tag); 367 m->m_flags &= ~M_IP_NEXTHOP; 368 } 369 if (dest.s_addr != rtdest.s_addr && 370 ip_findroute(&nh, dest, m) != 0) 371 return (NULL); /* icmp unreach already sent */ 372 } 373 374 passout: 375 /* 376 * Step 6: send off the packet 377 */ 378 ip_len = ntohs(ip->ip_len); 379 ip_off = ntohs(ip->ip_off); 380 381 bzero(&dst, sizeof(dst)); 382 dst.sin_family = AF_INET; 383 dst.sin_len = sizeof(dst); 384 if (nh->nh_flags & NHF_GATEWAY) 385 dst.sin_addr = nh->gw4_sa.sin_addr; 386 else 387 dst.sin_addr = dest; 388 389 /* 390 * Check if packet fits MTU or if hardware will fragment for us 391 */ 392 if (ip_len <= nh->nh_mtu) { 393 /* 394 * Avoid confusing lower layers. 395 */ 396 m_clrprotoflags(m); 397 /* 398 * Send off the packet via outgoing interface 399 */ 400 IP_PROBE(send, NULL, NULL, ip, nh->nh_ifp, ip, NULL); 401 error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m, 402 (struct sockaddr *)&dst, NULL); 403 } else { 404 /* 405 * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery 406 */ 407 if (ip_off & IP_DF) { 408 IPSTAT_INC(ips_cantfrag); 409 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 410 0, nh->nh_mtu); 411 goto consumed; 412 } else { 413 /* 414 * We have to fragment the packet 415 */ 416 m->m_pkthdr.csum_flags |= CSUM_IP; 417 if (ip_fragment(ip, &m, nh->nh_mtu, 418 nh->nh_ifp->if_hwassist) != 0) 419 goto drop; 420 KASSERT(m != NULL, ("null mbuf and no error")); 421 /* 422 * Send off the fragments via outgoing interface 423 */ 424 error = 0; 425 do { 426 m0 = m->m_nextpkt; 427 m->m_nextpkt = NULL; 428 /* 429 * Avoid confusing lower layers. 430 */ 431 m_clrprotoflags(m); 432 433 IP_PROBE(send, NULL, NULL, 434 mtod(m, struct ip *), nh->nh_ifp, 435 mtod(m, struct ip *), NULL); 436 error = (*nh->nh_ifp->if_output)(nh->nh_ifp, m, 437 (struct sockaddr *)&dst, NULL); 438 if (error) 439 break; 440 } while ((m = m0) != NULL); 441 if (error) { 442 /* Reclaim remaining fragments */ 443 for (m = m0; m; m = m0) { 444 m0 = m->m_nextpkt; 445 m_freem(m); 446 } 447 } else 448 IPSTAT_INC(ips_fragmented); 449 } 450 } 451 452 if (error != 0) 453 IPSTAT_INC(ips_odropped); 454 else { 455 IPSTAT_INC(ips_forward); 456 IPSTAT_INC(ips_fastforward); 457 } 458 consumed: 459 return NULL; 460 drop: 461 if (m) 462 m_freem(m); 463 return NULL; 464 } 465