ip_fastfwd.c revision 129017
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: head/sys/netinet/ip_fastfwd.c 129017 2004-05-06 18:46:03Z andre $ 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_ipdn.h" 80#include "opt_ipdivert.h" 81#include "opt_ipfilter.h" 82#include "opt_ipstealth.h" 83#include "opt_mac.h" 84#include "opt_pfil_hooks.h" 85 86#include <sys/param.h> 87#include <sys/systm.h> 88#include <sys/kernel.h> 89#include <sys/mac.h> 90#include <sys/malloc.h> 91#include <sys/mbuf.h> 92#include <sys/protosw.h> 93#include <sys/socket.h> 94#include <sys/sysctl.h> 95 96#include <net/pfil.h> 97#include <net/if.h> 98#include <net/if_types.h> 99#include <net/if_var.h> 100#include <net/if_dl.h> 101#include <net/route.h> 102 103#include <netinet/in.h> 104#include <netinet/in_systm.h> 105#include <netinet/in_var.h> 106#include <netinet/ip.h> 107#include <netinet/ip_var.h> 108#include <netinet/ip_icmp.h> 109 110#include <machine/in_cksum.h> 111 112#include <netinet/ip_fw.h> 113#include <netinet/ip_divert.h> 114#include <netinet/ip_dummynet.h> 115 116static int ipfastforward_active = 0; 117SYSCTL_INT(_net_inet_ip, OID_AUTO, fastforwarding, CTLFLAG_RW, 118 &ipfastforward_active, 0, "Enable fast IP forwarding"); 119 120static struct sockaddr_in * 121ip_findroute(struct route *ro, in_addr_t dest, struct mbuf *m) 122{ 123 struct sockaddr_in *dst; 124 struct rtentry *rt; 125 126 /* 127 * Find route to destination. 128 */ 129 bzero(ro, sizeof(*ro)); 130 dst = (struct sockaddr_in *)&ro->ro_dst; 131 dst->sin_family = AF_INET; 132 dst->sin_len = sizeof(*dst); 133 dst->sin_addr.s_addr = dest; 134 rtalloc_ign(ro, RTF_CLONING); 135 136 /* 137 * Route there and interface still up? 138 */ 139 rt = ro->ro_rt; 140 if (rt && (rt->rt_flags & RTF_UP) && 141 (rt->rt_ifp->if_flags & IFF_UP) && 142 (rt->rt_ifp->if_flags & IFF_RUNNING)) { 143 if (rt->rt_flags & RTF_GATEWAY) 144 dst = (struct sockaddr_in *)rt->rt_gateway; 145 } else { 146 ipstat.ips_noroute++; 147 ipstat.ips_cantforward++; 148 if (rt) 149 RTFREE(rt); 150 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL); 151 return NULL; 152 } 153 return dst; 154} 155 156/* 157 * Try to forward a packet based on the destination address. 158 * This is a fast path optimized for the plain forwarding case. 159 * If the packet is handled (and consumed) here then we return 1; 160 * otherwise 0 is returned and the packet should be delivered 161 * to ip_input for full processing. 162 */ 163int 164ip_fastforward(struct mbuf *m) 165{ 166 struct ip *ip; 167 struct mbuf *m0 = NULL; 168#ifdef IPDIVERT 169 struct ip *tip; 170 struct mbuf *clone = NULL; 171#endif 172 struct route ro; 173 struct sockaddr_in *dst = NULL; 174 struct in_ifaddr *ia = NULL; 175 struct ifaddr *ifa = NULL; 176 struct ifnet *ifp; 177 struct ip_fw_args args; 178 in_addr_t odest, dest; 179 u_short sum, ip_len; 180 int error = 0; 181 int hlen, ipfw, mtu; 182 183 /* 184 * Are we active and forwarding packets? 185 */ 186 if (!ipfastforward_active || !ipforwarding) 187 return 0; 188 189 M_ASSERTVALID(m); 190 M_ASSERTPKTHDR(m); 191 192 ro.ro_rt = NULL; 193 194 /* 195 * Step 1: check for packet drop conditions (and sanity checks) 196 */ 197 198 /* 199 * Is entire packet big enough? 200 */ 201 if (m->m_pkthdr.len < sizeof(struct ip)) { 202 ipstat.ips_tooshort++; 203 goto drop; 204 } 205 206 /* 207 * Is first mbuf large enough for ip header and is header present? 208 */ 209 if (m->m_len < sizeof (struct ip) && 210 (m = m_pullup(m, sizeof (struct ip))) == 0) { 211 ipstat.ips_toosmall++; 212 goto drop; 213 } 214 215 ip = mtod(m, struct ip *); 216 217 /* 218 * Is it IPv4? 219 */ 220 if (ip->ip_v != IPVERSION) { 221 ipstat.ips_badvers++; 222 goto drop; 223 } 224 225 /* 226 * Is IP header length correct and is it in first mbuf? 227 */ 228 hlen = ip->ip_hl << 2; 229 if (hlen < sizeof(struct ip)) { /* minimum header length */ 230 ipstat.ips_badlen++; 231 goto drop; 232 } 233 if (hlen > m->m_len) { 234 if ((m = m_pullup(m, hlen)) == 0) { 235 ipstat.ips_badhlen++; 236 goto drop; 237 } 238 ip = mtod(m, struct ip *); 239 } 240 241 /* 242 * Checksum correct? 243 */ 244 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) 245 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 246 else { 247 if (hlen == sizeof(struct ip)) 248 sum = in_cksum_hdr(ip); 249 else 250 sum = in_cksum(m, hlen); 251 } 252 if (sum) { 253 ipstat.ips_badsum++; 254 goto drop; 255 } 256 m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID); 257 258 ip_len = ntohs(ip->ip_len); 259 260 /* 261 * Is IP length longer than packet we have got? 262 */ 263 if (m->m_pkthdr.len < ip_len) { 264 ipstat.ips_tooshort++; 265 goto drop; 266 } 267 268 /* 269 * Is packet longer than IP header tells us? If yes, truncate packet. 270 */ 271 if (m->m_pkthdr.len > ip_len) { 272 if (m->m_len == m->m_pkthdr.len) { 273 m->m_len = ip_len; 274 m->m_pkthdr.len = ip_len; 275 } else 276 m_adj(m, ip_len - m->m_pkthdr.len); 277 } 278 279 /* 280 * Is packet from or to 127/8? 281 */ 282 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 283 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 284 ipstat.ips_badaddr++; 285 goto drop; 286 } 287 288 /* 289 * Step 2: fallback conditions to normal ip_input path processing 290 */ 291 292 /* 293 * Only IP packets without options 294 */ 295 if (ip->ip_hl != (sizeof(struct ip) >> 2)) { 296 if (ip_doopts == 1) 297 return 0; 298 else if (ip_doopts == 2) { 299 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_FILTER_PROHIB, 300 0, NULL); 301 return 1; 302 } 303 /* else ignore IP options and continue */ 304 } 305 306 /* 307 * Only unicast IP, not from loopback, no L2 or IP broadcast, 308 * no multicast, no INADDR_ANY 309 * 310 * XXX: Probably some of these checks could be direct drop 311 * conditions. However it is not clear whether there are some 312 * hacks or obscure behaviours which make it neccessary to 313 * let ip_input handle it. We play safe here and let ip_input 314 * deal with it until it is proven that we can directly drop it. 315 */ 316 if ((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_dst.s_addr == INADDR_ANY ) 322 return 0; 323 324 /* 325 * Is it for a local address on this host? 326 */ 327 LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 328 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 329 return 0; 330 } 331 332 /* 333 * Or is it for a local IP broadcast address on this host? 334 */ 335 if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 336 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 337 if (ifa->ifa_addr->sa_family != AF_INET) 338 continue; 339 ia = ifatoia(ifa); 340 if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr) 341 return 0; 342 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 343 ip->ip_dst.s_addr) 344 return 0; 345 } 346 } 347 ipstat.ips_total++; 348 349 /* 350 * Step 3: incoming packet firewall processing 351 */ 352 353 /* 354 * Convert to host representation 355 */ 356 ip->ip_len = ntohs(ip->ip_len); 357 ip->ip_off = ntohs(ip->ip_off); 358 359 odest = dest = ip->ip_dst.s_addr; 360#ifdef PFIL_HOOKS 361 /* 362 * Run through list of ipfilter hooks for input packets 363 */ 364 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN) || 365 m == NULL) 366 return 1; 367 368 M_ASSERTVALID(m); 369 M_ASSERTPKTHDR(m); 370 371 ip = mtod(m, struct ip *); /* m may have changed by pfil hook */ 372 dest = ip->ip_dst.s_addr; 373#endif 374 375 /* 376 * Run through ipfw for input packets 377 */ 378 if (fw_enable && IPFW_LOADED) { 379 bzero(&args, sizeof(args)); 380 args.m = m; 381 382 ipfw = ip_fw_chk_ptr(&args); 383 m = args.m; 384 385 M_ASSERTVALID(m); 386 M_ASSERTPKTHDR(m); 387 388 /* 389 * Packet denied, drop it 390 */ 391 if ((ipfw & IP_FW_PORT_DENY_FLAG) || m == NULL) 392 goto drop; 393 /* 394 * Send packet to the appropriate pipe 395 */ 396 if (DUMMYNET_LOADED && (ipfw & IP_FW_PORT_DYNT_FLAG) != 0) { 397 ip_dn_io_ptr(m, ipfw & 0xffff, DN_TO_IP_IN, &args); 398 return 1; 399 } 400#ifdef IPDIVERT 401 /* 402 * Divert packet 403 */ 404 if (ipfw != 0 && (ipfw & IP_FW_PORT_DYNT_FLAG) == 0) { 405 /* 406 * See if this is a fragment 407 */ 408 if (ip->ip_off & (IP_MF | IP_OFFMASK)) 409 goto droptoours; 410 /* 411 * Tee packet 412 */ 413 if ((ipfw & IP_FW_PORT_TEE_FLAG) != 0) 414 clone = divert_clone(m); 415 else 416 clone = m; 417 if (clone == NULL) 418 goto passin; 419 420 /* 421 * Delayed checksums are not compatible 422 */ 423 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 424 in_delayed_cksum(m); 425 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 426 } 427 /* 428 * Restore packet header fields to original values 429 */ 430 tip = mtod(m, struct ip *); 431 tip->ip_len = htons(tip->ip_len); 432 tip->ip_off = htons(tip->ip_off); 433 /* 434 * Deliver packet to divert input routine 435 */ 436 divert_packet(m, 0); 437 /* 438 * If this was not tee, we are done 439 */ 440 m = clone; 441 if ((ipfw & IP_FW_PORT_TEE_FLAG) == 0) 442 return 1; 443 /* Continue if it was tee */ 444 goto passin; 445 } 446#endif 447 if (ipfw == 0 && args.next_hop != NULL) { 448 dest = args.next_hop->sin_addr.s_addr; 449 goto passin; 450 } 451 /* 452 * Let through or not? 453 */ 454 if (ipfw != 0) 455 goto drop; 456 } 457passin: 458 ip = mtod(m, struct ip *); /* if m changed during fw processing */ 459 460 /* 461 * Destination address changed? 462 */ 463 if (odest != dest) { 464 /* 465 * Is it now for a local address on this host? 466 */ 467 LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 468 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 469 goto forwardlocal; 470 } 471 /* 472 * Go on with new destination address 473 */ 474 } 475 476 /* 477 * Step 4: decrement TTL and look up route 478 */ 479 480 /* 481 * Check TTL 482 */ 483#ifdef IPSTEALTH 484 if (!ipstealth) { 485#endif 486 if (ip->ip_ttl <= IPTTLDEC) { 487 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, NULL); 488 return 1; 489 } 490 491 /* 492 * Decrement the TTL and incrementally change the checksum. 493 * Don't bother doing this with hw checksum offloading. 494 */ 495 ip->ip_ttl -= IPTTLDEC; 496 if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8)) 497 ip->ip_sum -= ~htons(IPTTLDEC << 8); 498 else 499 ip->ip_sum += htons(IPTTLDEC << 8); 500#ifdef IPSTEALTH 501 } 502#endif 503 504 /* 505 * Find route to destination. 506 */ 507 if ((dst = ip_findroute(&ro, dest, m)) == NULL) 508 return 1; /* icmp unreach already sent */ 509 ifp = ro.ro_rt->rt_ifp; 510 511 /* 512 * Step 5: outgoing firewall packet processing 513 */ 514 515#ifdef PFIL_HOOKS 516 /* 517 * Run through list of hooks for output packets. 518 */ 519 if (pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT) || m == NULL) { 520 goto consumed; 521 } 522 523 M_ASSERTVALID(m); 524 M_ASSERTPKTHDR(m); 525 526 ip = mtod(m, struct ip *); 527 dest = ip->ip_dst.s_addr; 528#endif 529 if (fw_enable && IPFW_LOADED && !args.next_hop) { 530 bzero(&args, sizeof(args)); 531 args.m = m; 532 args.oif = ifp; 533 534 ipfw = ip_fw_chk_ptr(&args); 535 m = args.m; 536 537 M_ASSERTVALID(m); 538 M_ASSERTPKTHDR(m); 539 540 if ((ipfw & IP_FW_PORT_DENY_FLAG) || m == NULL) 541 goto drop; 542 543 if (DUMMYNET_LOADED && (ipfw & IP_FW_PORT_DYNT_FLAG) != 0) { 544 /* 545 * XXX note: if the ifp or rt entry are deleted 546 * while a pkt is in dummynet, we are in trouble! 547 */ 548 args.ro = &ro; /* dummynet does not save it */ 549 args.dst = dst; 550 551 ip_dn_io_ptr(m, ipfw & 0xffff, DN_TO_IP_OUT, &args); 552 goto consumed; 553 } 554#ifdef IPDIVERT 555 if (ipfw != 0 && (ipfw & IP_FW_PORT_DYNT_FLAG) == 0) { 556 /* 557 * See if this is a fragment 558 */ 559 if (ip->ip_off & (IP_MF | IP_OFFMASK)) 560 goto droptoours; 561 /* 562 * Tee packet 563 */ 564 if ((ipfw & IP_FW_PORT_TEE_FLAG) != 0) 565 clone = divert_clone(m); 566 else 567 clone = m; 568 if (clone == NULL) 569 goto passout; 570 571 /* 572 * Delayed checksums are not compatible with divert 573 */ 574 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 575 in_delayed_cksum(m); 576 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 577 } 578 /* 579 * Restore packet header fields to original values 580 */ 581 tip = mtod(m, struct ip *); 582 tip->ip_len = htons(tip->ip_len); 583 tip->ip_off = htons(tip->ip_off); 584 /* 585 * Deliver packet to divert input routine 586 */ 587 divert_packet(m, 0); 588 /* 589 * If this was not tee, we are done 590 */ 591 m = clone; 592 if ((ipfw & IP_FW_PORT_TEE_FLAG) == 0) { 593 goto consumed; 594 } 595 /* Continue if it was tee */ 596 goto passout; 597 } 598#endif 599 if (ipfw == 0 && args.next_hop != NULL) { 600 dest = args.next_hop->sin_addr.s_addr; 601 goto passout; 602 } 603 /* 604 * Let through or not? 605 */ 606 if (ipfw != 0) 607 goto drop; 608 } 609passout: 610 ip = mtod(m, struct ip *); 611 612 /* 613 * Destination address changed? 614 */ 615 if (odest != dest) { 616 /* 617 * Is it now for a local address on this host? 618 */ 619 LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 620 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) { 621forwardlocal: 622 if (args.next_hop) { 623 struct m_tag *mtag = m_tag_get( 624 PACKET_TAG_IPFORWARD, 625 sizeof(struct sockaddr_in *), 626 M_NOWAIT); 627 if (mtag == NULL) { 628 goto drop; 629 } 630 *(struct sockaddr_in **)(mtag+1) = 631 args.next_hop; 632 m_tag_prepend(m, mtag); 633 } 634#ifdef IPDIVERT 635droptoours: /* Used for DIVERT */ 636#endif 637 /* for ip_input */ 638 m->m_flags |= M_FASTFWD_OURS; 639 640 /* ip still points to the real packet */ 641 ip->ip_len = htons(ip->ip_len); 642 ip->ip_off = htons(ip->ip_off); 643 644 /* 645 * Return packet for processing by ip_input 646 */ 647 if (ro.ro_rt) 648 RTFREE(ro.ro_rt); 649 return 0; 650 } 651 } 652 /* 653 * Redo route lookup with new destination address 654 */ 655 RTFREE(ro.ro_rt); 656 if ((dst = ip_findroute(&ro, dest, m)) == NULL) 657 return 1; /* icmp unreach already sent */ 658 ifp = ro.ro_rt->rt_ifp; 659 } 660 661 /* 662 * Step 6: send off the packet 663 */ 664 665 /* 666 * Check if route is dampned (when ARP is unable to resolve) 667 */ 668 if ((ro.ro_rt->rt_flags & RTF_REJECT) && 669 ro.ro_rt->rt_rmx.rmx_expire >= time_second) { 670 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL); 671 goto consumed; 672 } 673 674 /* 675 * Check if there is enough space in the interface queue 676 */ 677 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 678 ifp->if_snd.ifq_maxlen) { 679 ipstat.ips_odropped++; 680 /* would send source quench here but that is depreciated */ 681 goto drop; 682 } 683 684 /* 685 * Check if media link state of interface is not down 686 */ 687 if (ifp->if_link_state == LINK_STATE_DOWN) { 688 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, NULL); 689 goto consumed; 690 } 691 692 /* 693 * Check if packet fits MTU or if hardware will fragement for us 694 */ 695 if (ro.ro_rt->rt_rmx.rmx_mtu) 696 mtu = min(ro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu); 697 else 698 mtu = ifp->if_mtu; 699 700 if (ip->ip_len <= mtu || 701 (ifp->if_hwassist & CSUM_FRAGMENT && (ip->ip_off & IP_DF) == 0)) { 702 /* 703 * Restore packet header fields to original values 704 */ 705 ip->ip_len = htons(ip->ip_len); 706 ip->ip_off = htons(ip->ip_off); 707 /* 708 * Send off the packet via outgoing interface 709 */ 710 error = (*ifp->if_output)(ifp, m, 711 (struct sockaddr *)dst, ro.ro_rt); 712 } else { 713 /* 714 * Handle EMSGSIZE with icmp reply needfrag for TCP MTU discovery 715 */ 716 if (ip->ip_off & IP_DF) { 717 ipstat.ips_cantfrag++; 718 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 719 0, ifp); 720 goto consumed; 721 } else { 722 /* 723 * We have to fragement the packet 724 */ 725 m->m_pkthdr.csum_flags |= CSUM_IP; 726 /* 727 * ip_fragment expects ip_len and ip_off in host byte 728 * order but returns all packets in network byte order 729 */ 730 if (ip_fragment(ip, &m, mtu, ifp->if_hwassist, 731 (~ifp->if_hwassist & CSUM_DELAY_IP))) { 732 goto drop; 733 } 734 KASSERT(m != NULL, ("null mbuf and no error")); 735 /* 736 * Send off the fragments via outgoing interface 737 */ 738 error = 0; 739 do { 740 m0 = m->m_nextpkt; 741 m->m_nextpkt = NULL; 742 743 error = (*ifp->if_output)(ifp, m, 744 (struct sockaddr *)dst, ro.ro_rt); 745 if (error) 746 break; 747 } while ((m = m0) != NULL); 748 if (error) { 749 /* Reclaim remaining fragments */ 750 for (; m; m = m0) { 751 m0 = m->m_nextpkt; 752 m->m_nextpkt = NULL; 753 m_freem(m); 754 } 755 } else 756 ipstat.ips_fragmented++; 757 } 758 } 759 760 if (error != 0) 761 ipstat.ips_odropped++; 762 else { 763 ro.ro_rt->rt_rmx.rmx_pksent++; 764 ipstat.ips_forward++; 765 ipstat.ips_fastforward++; 766 } 767consumed: 768 RTFREE(ro.ro_rt); 769 return 1; 770drop: 771 if (m) 772 m_freem(m); 773 if (ro.ro_rt) 774 RTFREE(ro.ro_rt); 775 return 1; 776} 777