1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993 3 * The Regents of the University of California. 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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_ipfw.h" 36 #include "opt_ipsec.h" 37 #include "opt_route.h" 38 #include "opt_mbuf_stress_test.h" 39 #include "opt_mpath.h" 40 #include "opt_sctp.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/malloc.h> 46 #include <sys/mbuf.h> 47 #include <sys/priv.h> 48 #include <sys/proc.h> 49 #include <sys/protosw.h> 50 #include <sys/socket.h> 51 #include <sys/socketvar.h> 52 #include <sys/sysctl.h> 53 #include <sys/ucred.h> 54 55 #include <net/if.h> 56 #include <net/if_llatbl.h> 57 #include <net/netisr.h> 58 #include <net/pfil.h> 59 #include <net/route.h> 60 #include <net/flowtable.h> 61 #ifdef RADIX_MPATH 62 #include <net/radix_mpath.h> 63 #endif 64 #include <net/vnet.h> 65 66 #include <netinet/in.h> 67 #include <netinet/in_systm.h> 68 #include <netinet/ip.h> 69 #include <netinet/in_pcb.h> 70 #include <netinet/in_var.h> 71 #include <netinet/ip_var.h> 72 #include <netinet/ip_options.h> 73 #ifdef SCTP 74 #include <netinet/sctp.h> 75 #include <netinet/sctp_crc32.h> 76 #endif 77 78 #ifdef IPSEC 79 #include <netinet/ip_ipsec.h> 80 #include <netipsec/ipsec.h> 81 #endif /* IPSEC*/ 82 83 #include <machine/in_cksum.h> 84 85 #include <security/mac/mac_framework.h> 86 87 VNET_DEFINE(u_short, ip_id); 88 89 #ifdef MBUF_STRESS_TEST 90 int mbuf_frag_size = 0; 91 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 92 &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 93 #endif 94 95 static void ip_mloopback 96 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 97 98 99 extern int in_mcast_loop; 100 extern struct protosw inetsw[]; 101 102 /* 103 * IP output. The packet in mbuf chain m contains a skeletal IP 104 * header (with len, off, ttl, proto, tos, src, dst). 105 * ip_len and ip_off are in host format. 106 * The mbuf chain containing the packet will be freed. 107 * The mbuf opt, if present, will not be freed. 108 * In the IP forwarding case, the packet will arrive with options already 109 * inserted, so must have a NULL opt pointer. 110 */ 111 int 112 ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags, 113 struct ip_moptions *imo, struct inpcb *inp) 114 { 115 struct ip *ip; 116 struct ifnet *ifp = NULL; /* keep compiler happy */ 117 struct mbuf *m0; 118 int hlen = sizeof (struct ip); 119 int mtu; 120 int n; /* scratchpad */ 121 int error = 0; 122 int nortfree = 0; 123 struct sockaddr_in *dst; 124 struct in_ifaddr *ia = NULL; 125 int isbroadcast, sw_csum; 126 struct route iproute; 127 struct rtentry *rte; /* cache for ro->ro_rt */ 128 struct in_addr odst; 129 #ifdef IPFIREWALL_FORWARD 130 struct m_tag *fwd_tag = NULL; 131 #endif 132 #ifdef IPSEC 133 int no_route_but_check_spd = 0; 134 #endif 135 M_ASSERTPKTHDR(m); 136 137 if (inp != NULL) { 138 INP_LOCK_ASSERT(inp); 139 M_SETFIB(m, inp->inp_inc.inc_fibnum); 140 if (inp->inp_flags & (INP_HW_FLOWID|INP_SW_FLOWID)) { 141 m->m_pkthdr.flowid = inp->inp_flowid; 142 m->m_flags |= M_FLOWID; 143 } 144 } 145 146 if (ro == NULL) { 147 ro = &iproute; 148 bzero(ro, sizeof (*ro)); 149 150 #ifdef FLOWTABLE 151 { 152 struct flentry *fle; 153 154 /* 155 * The flow table returns route entries valid for up to 30 156 * seconds; we rely on the remainder of ip_output() taking no 157 * longer than that long for the stability of ro_rt. The 158 * flow ID assignment must have happened before this point. 159 */ 160 if ((fle = flowtable_lookup_mbuf(V_ip_ft, m, AF_INET)) != NULL) { 161 flow_to_route(fle, ro); 162 nortfree = 1; 163 } 164 } 165 #endif 166 } 167 168 if (opt) { 169 int len = 0; 170 m = ip_insertoptions(m, opt, &len); 171 if (len != 0) 172 hlen = len; /* ip->ip_hl is updated above */ 173 } 174 ip = mtod(m, struct ip *); 175 176 /* 177 * Fill in IP header. If we are not allowing fragmentation, 178 * then the ip_id field is meaningless, but we don't set it 179 * to zero. Doing so causes various problems when devices along 180 * the path (routers, load balancers, firewalls, etc.) illegally 181 * disable DF on our packet. Note that a 16-bit counter 182 * will wrap around in less than 10 seconds at 100 Mbit/s on a 183 * medium with MTU 1500. See Steven M. Bellovin, "A Technique 184 * for Counting NATted Hosts", Proc. IMW'02, available at 185 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>. 186 */ 187 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 188 ip->ip_v = IPVERSION; 189 ip->ip_hl = hlen >> 2; 190 ip->ip_id = ip_newid(); 191 IPSTAT_INC(ips_localout); 192 } else { 193 /* Header already set, fetch hlen from there */ 194 hlen = ip->ip_hl << 2; 195 } 196 197 dst = (struct sockaddr_in *)&ro->ro_dst; 198 again: 199 /* 200 * If there is a cached route, 201 * check that it is to the same destination 202 * and is still up. If not, free it and try again. 203 * The address family should also be checked in case of sharing the 204 * cache with IPv6. 205 */ 206 rte = ro->ro_rt; 207 if (rte && ((rte->rt_flags & RTF_UP) == 0 || 208 rte->rt_ifp == NULL || 209 !RT_LINK_IS_UP(rte->rt_ifp) || 210 dst->sin_family != AF_INET || 211 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 212 if (!nortfree) 213 RTFREE(rte); 214 rte = ro->ro_rt = (struct rtentry *)NULL; 215 ro->ro_lle = (struct llentry *)NULL; 216 } 217 #ifdef IPFIREWALL_FORWARD 218 if (rte == NULL && fwd_tag == NULL) { 219 #else 220 if (rte == NULL) { 221 #endif 222 bzero(dst, sizeof(*dst)); 223 dst->sin_family = AF_INET; 224 dst->sin_len = sizeof(*dst); 225 dst->sin_addr = ip->ip_dst; 226 } 227 /* 228 * If routing to interface only, short circuit routing lookup. 229 * The use of an all-ones broadcast address implies this; an 230 * interface is specified by the broadcast address of an interface, 231 * or the destination address of a ptp interface. 232 */ 233 if (flags & IP_SENDONES) { 234 if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL && 235 (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) { 236 IPSTAT_INC(ips_noroute); 237 error = ENETUNREACH; 238 goto bad; 239 } 240 ip->ip_dst.s_addr = INADDR_BROADCAST; 241 dst->sin_addr = ip->ip_dst; 242 ifp = ia->ia_ifp; 243 ip->ip_ttl = 1; 244 isbroadcast = 1; 245 } else if (flags & IP_ROUTETOIF) { 246 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 247 (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0))) == NULL) { 248 IPSTAT_INC(ips_noroute); 249 error = ENETUNREACH; 250 goto bad; 251 } 252 ifp = ia->ia_ifp; 253 ip->ip_ttl = 1; 254 isbroadcast = in_broadcast(dst->sin_addr, ifp); 255 } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 256 imo != NULL && imo->imo_multicast_ifp != NULL) { 257 /* 258 * Bypass the normal routing lookup for multicast 259 * packets if the interface is specified. 260 */ 261 ifp = imo->imo_multicast_ifp; 262 IFP_TO_IA(ifp, ia); 263 isbroadcast = 0; /* fool gcc */ 264 } else { 265 /* 266 * We want to do any cloning requested by the link layer, 267 * as this is probably required in all cases for correct 268 * operation (as it is for ARP). 269 */ 270 if (rte == NULL) { 271 #ifdef RADIX_MPATH 272 rtalloc_mpath_fib(ro, 273 ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr), 274 inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m)); 275 #else 276 in_rtalloc_ign(ro, 0, 277 inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m)); 278 #endif 279 rte = ro->ro_rt; 280 } 281 if (rte == NULL || 282 rte->rt_ifp == NULL || 283 !RT_LINK_IS_UP(rte->rt_ifp)) { 284 #ifdef IPSEC 285 /* 286 * There is no route for this packet, but it is 287 * possible that a matching SPD entry exists. 288 */ 289 no_route_but_check_spd = 1; 290 mtu = 0; /* Silence GCC warning. */ 291 goto sendit; 292 #endif 293 IPSTAT_INC(ips_noroute); 294 error = EHOSTUNREACH; 295 goto bad; 296 } 297 ia = ifatoia(rte->rt_ifa); 298 ifa_ref(&ia->ia_ifa); 299 ifp = rte->rt_ifp; 300 rte->rt_rmx.rmx_pksent++; 301 if (rte->rt_flags & RTF_GATEWAY) 302 dst = (struct sockaddr_in *)rte->rt_gateway; 303 if (rte->rt_flags & RTF_HOST) 304 isbroadcast = (rte->rt_flags & RTF_BROADCAST); 305 else 306 isbroadcast = in_broadcast(dst->sin_addr, ifp); 307 } 308 /* 309 * Calculate MTU. If we have a route that is up, use that, 310 * otherwise use the interface's MTU. 311 */ 312 if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST))) { 313 /* 314 * This case can happen if the user changed the MTU 315 * of an interface after enabling IP on it. Because 316 * most netifs don't keep track of routes pointing to 317 * them, there is no way for one to update all its 318 * routes when the MTU is changed. 319 */ 320 if (rte->rt_rmx.rmx_mtu > ifp->if_mtu) 321 rte->rt_rmx.rmx_mtu = ifp->if_mtu; 322 mtu = rte->rt_rmx.rmx_mtu; 323 } else { 324 mtu = ifp->if_mtu; 325 } 326 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 327 m->m_flags |= M_MCAST; 328 /* 329 * IP destination address is multicast. Make sure "dst" 330 * still points to the address in "ro". (It may have been 331 * changed to point to a gateway address, above.) 332 */ 333 dst = (struct sockaddr_in *)&ro->ro_dst; 334 /* 335 * See if the caller provided any multicast options 336 */ 337 if (imo != NULL) { 338 ip->ip_ttl = imo->imo_multicast_ttl; 339 if (imo->imo_multicast_vif != -1) 340 ip->ip_src.s_addr = 341 ip_mcast_src ? 342 ip_mcast_src(imo->imo_multicast_vif) : 343 INADDR_ANY; 344 } else 345 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 346 /* 347 * Confirm that the outgoing interface supports multicast. 348 */ 349 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 350 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 351 IPSTAT_INC(ips_noroute); 352 error = ENETUNREACH; 353 goto bad; 354 } 355 } 356 /* 357 * If source address not specified yet, use address 358 * of outgoing interface. 359 */ 360 if (ip->ip_src.s_addr == INADDR_ANY) { 361 /* Interface may have no addresses. */ 362 if (ia != NULL) 363 ip->ip_src = IA_SIN(ia)->sin_addr; 364 } 365 366 if ((imo == NULL && in_mcast_loop) || 367 (imo && imo->imo_multicast_loop)) { 368 /* 369 * Loop back multicast datagram if not expressly 370 * forbidden to do so, even if we are not a member 371 * of the group; ip_input() will filter it later, 372 * thus deferring a hash lookup and mutex acquisition 373 * at the expense of a cheap copy using m_copym(). 374 */ 375 ip_mloopback(ifp, m, dst, hlen); 376 } else { 377 /* 378 * If we are acting as a multicast router, perform 379 * multicast forwarding as if the packet had just 380 * arrived on the interface to which we are about 381 * to send. The multicast forwarding function 382 * recursively calls this function, using the 383 * IP_FORWARDING flag to prevent infinite recursion. 384 * 385 * Multicasts that are looped back by ip_mloopback(), 386 * above, will be forwarded by the ip_input() routine, 387 * if necessary. 388 */ 389 if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) { 390 /* 391 * If rsvp daemon is not running, do not 392 * set ip_moptions. This ensures that the packet 393 * is multicast and not just sent down one link 394 * as prescribed by rsvpd. 395 */ 396 if (!V_rsvp_on) 397 imo = NULL; 398 if (ip_mforward && 399 ip_mforward(ip, ifp, m, imo) != 0) { 400 m_freem(m); 401 goto done; 402 } 403 } 404 } 405 406 /* 407 * Multicasts with a time-to-live of zero may be looped- 408 * back, above, but must not be transmitted on a network. 409 * Also, multicasts addressed to the loopback interface 410 * are not sent -- the above call to ip_mloopback() will 411 * loop back a copy. ip_input() will drop the copy if 412 * this host does not belong to the destination group on 413 * the loopback interface. 414 */ 415 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 416 m_freem(m); 417 goto done; 418 } 419 420 goto sendit; 421 } 422 423 /* 424 * If the source address is not specified yet, use the address 425 * of the outoing interface. 426 */ 427 if (ip->ip_src.s_addr == INADDR_ANY) { 428 /* Interface may have no addresses. */ 429 if (ia != NULL) { 430 ip->ip_src = IA_SIN(ia)->sin_addr; 431 } 432 } 433 434 /* 435 * Verify that we have any chance at all of being able to queue the 436 * packet or packet fragments, unless ALTQ is enabled on the given 437 * interface in which case packetdrop should be done by queueing. 438 */ 439 n = ip->ip_len / mtu + 1; /* how many fragments ? */ 440 if ( 441 #ifdef ALTQ 442 (!ALTQ_IS_ENABLED(&ifp->if_snd)) && 443 #endif /* ALTQ */ 444 (ifp->if_snd.ifq_len + n) >= ifp->if_snd.ifq_maxlen ) { 445 error = ENOBUFS; 446 IPSTAT_INC(ips_odropped); 447 ifp->if_snd.ifq_drops += n; 448 goto bad; 449 } 450 451 /* 452 * Look for broadcast address and 453 * verify user is allowed to send 454 * such a packet. 455 */ 456 if (isbroadcast) { 457 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 458 error = EADDRNOTAVAIL; 459 goto bad; 460 } 461 if ((flags & IP_ALLOWBROADCAST) == 0) { 462 error = EACCES; 463 goto bad; 464 } 465 /* don't allow broadcast messages to be fragmented */ 466 if (ip->ip_len > mtu) { 467 error = EMSGSIZE; 468 goto bad; 469 } 470 m->m_flags |= M_BCAST; 471 } else { 472 m->m_flags &= ~M_BCAST; 473 } 474 475 sendit: 476 #ifdef IPSEC 477 switch(ip_ipsec_output(&m, inp, &flags, &error)) { 478 case 1: 479 goto bad; 480 case -1: 481 goto done; 482 case 0: 483 default: 484 break; /* Continue with packet processing. */ 485 } 486 /* 487 * Check if there was a route for this packet; return error if not. 488 */ 489 if (no_route_but_check_spd) { 490 IPSTAT_INC(ips_noroute); 491 error = EHOSTUNREACH; 492 goto bad; 493 } 494 /* Update variables that are affected by ipsec4_output(). */ 495 ip = mtod(m, struct ip *); 496 hlen = ip->ip_hl << 2; 497 #endif /* IPSEC */ 498 499 /* Jump over all PFIL processing if hooks are not active. */ 500 if (!PFIL_HOOKED(&V_inet_pfil_hook)) 501 goto passout; 502 503 /* Run through list of hooks for output packets. */ 504 odst.s_addr = ip->ip_dst.s_addr; 505 error = pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, inp); 506 if (error != 0 || m == NULL) 507 goto done; 508 509 ip = mtod(m, struct ip *); 510 511 /* See if destination IP address was changed by packet filter. */ 512 if (odst.s_addr != ip->ip_dst.s_addr) { 513 m->m_flags |= M_SKIP_FIREWALL; 514 /* If destination is now ourself drop to ip_input(). */ 515 if (in_localip(ip->ip_dst)) { 516 m->m_flags |= M_FASTFWD_OURS; 517 if (m->m_pkthdr.rcvif == NULL) 518 m->m_pkthdr.rcvif = V_loif; 519 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 520 m->m_pkthdr.csum_flags |= 521 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 522 m->m_pkthdr.csum_data = 0xffff; 523 } 524 m->m_pkthdr.csum_flags |= 525 CSUM_IP_CHECKED | CSUM_IP_VALID; 526 #ifdef SCTP 527 if (m->m_pkthdr.csum_flags & CSUM_SCTP) 528 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 529 #endif 530 error = netisr_queue(NETISR_IP, m); 531 goto done; 532 } else 533 goto again; /* Redo the routing table lookup. */ 534 } 535 536 #ifdef IPFIREWALL_FORWARD 537 /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 538 if (m->m_flags & M_FASTFWD_OURS) { 539 if (m->m_pkthdr.rcvif == NULL) 540 m->m_pkthdr.rcvif = V_loif; 541 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 542 m->m_pkthdr.csum_flags |= 543 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 544 m->m_pkthdr.csum_data = 0xffff; 545 } 546 #ifdef SCTP 547 if (m->m_pkthdr.csum_flags & CSUM_SCTP) 548 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 549 #endif 550 m->m_pkthdr.csum_flags |= 551 CSUM_IP_CHECKED | CSUM_IP_VALID; 552 553 error = netisr_queue(NETISR_IP, m); 554 goto done; 555 } 556 /* Or forward to some other address? */ 557 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 558 if (fwd_tag) { 559 dst = (struct sockaddr_in *)&ro->ro_dst; 560 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 561 m->m_flags |= M_SKIP_FIREWALL; 562 m_tag_delete(m, fwd_tag); 563 goto again; 564 } 565 #endif /* IPFIREWALL_FORWARD */ 566 567 passout: 568 /* 127/8 must not appear on wire - RFC1122. */ 569 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 570 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 571 if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 572 IPSTAT_INC(ips_badaddr); 573 error = EADDRNOTAVAIL; 574 goto bad; 575 } 576 } 577 578 m->m_pkthdr.csum_flags |= CSUM_IP; 579 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 580 if (sw_csum & CSUM_DELAY_DATA) { 581 in_delayed_cksum(m); 582 sw_csum &= ~CSUM_DELAY_DATA; 583 } 584 #ifdef SCTP 585 if (sw_csum & CSUM_SCTP) { 586 sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 587 sw_csum &= ~CSUM_SCTP; 588 } 589 #endif 590 m->m_pkthdr.csum_flags &= ifp->if_hwassist; 591 592 /* 593 * If small enough for interface, or the interface will take 594 * care of the fragmentation for us, we can just send directly. 595 */ 596 if (ip->ip_len <= mtu || 597 (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 || 598 ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) { 599 ip->ip_len = htons(ip->ip_len); 600 ip->ip_off = htons(ip->ip_off); 601 ip->ip_sum = 0; 602 if (sw_csum & CSUM_DELAY_IP) 603 ip->ip_sum = in_cksum(m, hlen); 604 605 /* 606 * Record statistics for this interface address. 607 * With CSUM_TSO the byte/packet count will be slightly 608 * incorrect because we count the IP+TCP headers only 609 * once instead of for every generated packet. 610 */ 611 if (!(flags & IP_FORWARDING) && ia) { 612 if (m->m_pkthdr.csum_flags & CSUM_TSO) 613 ia->ia_ifa.if_opackets += 614 m->m_pkthdr.len / m->m_pkthdr.tso_segsz; 615 else 616 ia->ia_ifa.if_opackets++; 617 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 618 } 619 #ifdef MBUF_STRESS_TEST 620 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 621 m = m_fragment(m, M_DONTWAIT, mbuf_frag_size); 622 #endif 623 /* 624 * Reset layer specific mbuf flags 625 * to avoid confusing lower layers. 626 */ 627 m->m_flags &= ~(M_PROTOFLAGS); 628 error = (*ifp->if_output)(ifp, m, 629 (struct sockaddr *)dst, ro); 630 goto done; 631 } 632 633 /* Balk when DF bit is set or the interface didn't support TSO. */ 634 if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) { 635 error = EMSGSIZE; 636 IPSTAT_INC(ips_cantfrag); 637 goto bad; 638 } 639 640 /* 641 * Too large for interface; fragment if possible. If successful, 642 * on return, m will point to a list of packets to be sent. 643 */ 644 error = ip_fragment(ip, &m, mtu, ifp->if_hwassist, sw_csum); 645 if (error) 646 goto bad; 647 for (; m; m = m0) { 648 m0 = m->m_nextpkt; 649 m->m_nextpkt = 0; 650 if (error == 0) { 651 /* Record statistics for this interface address. */ 652 if (ia != NULL) { 653 ia->ia_ifa.if_opackets++; 654 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 655 } 656 /* 657 * Reset layer specific mbuf flags 658 * to avoid confusing upper layers. 659 */ 660 m->m_flags &= ~(M_PROTOFLAGS); 661 662 error = (*ifp->if_output)(ifp, m, 663 (struct sockaddr *)dst, ro); 664 } else 665 m_freem(m); 666 } 667 668 if (error == 0) 669 IPSTAT_INC(ips_fragmented); 670 671 done: 672 if (ro == &iproute && ro->ro_rt && !nortfree) { 673 RTFREE(ro->ro_rt); 674 } 675 if (ia != NULL) 676 ifa_free(&ia->ia_ifa); 677 return (error); 678 bad: 679 m_freem(m); 680 goto done; 681 } 682 683 /* 684 * Create a chain of fragments which fit the given mtu. m_frag points to the 685 * mbuf to be fragmented; on return it points to the chain with the fragments. 686 * Return 0 if no error. If error, m_frag may contain a partially built 687 * chain of fragments that should be freed by the caller. 688 * 689 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 690 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 691 */ 692 int 693 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 694 u_long if_hwassist_flags, int sw_csum) 695 { 696 int error = 0; 697 int hlen = ip->ip_hl << 2; 698 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 699 int off; 700 struct mbuf *m0 = *m_frag; /* the original packet */ 701 int firstlen; 702 struct mbuf **mnext; 703 int nfrags; 704 705 if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */ 706 IPSTAT_INC(ips_cantfrag); 707 return EMSGSIZE; 708 } 709 710 /* 711 * Must be able to put at least 8 bytes per fragment. 712 */ 713 if (len < 8) 714 return EMSGSIZE; 715 716 /* 717 * If the interface will not calculate checksums on 718 * fragmented packets, then do it here. 719 */ 720 if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 721 (if_hwassist_flags & CSUM_IP_FRAGS) == 0) { 722 in_delayed_cksum(m0); 723 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 724 } 725 #ifdef SCTP 726 if (m0->m_pkthdr.csum_flags & CSUM_SCTP && 727 (if_hwassist_flags & CSUM_IP_FRAGS) == 0) { 728 sctp_delayed_cksum(m0, hlen); 729 m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 730 } 731 #endif 732 if (len > PAGE_SIZE) { 733 /* 734 * Fragment large datagrams such that each segment 735 * contains a multiple of PAGE_SIZE amount of data, 736 * plus headers. This enables a receiver to perform 737 * page-flipping zero-copy optimizations. 738 * 739 * XXX When does this help given that sender and receiver 740 * could have different page sizes, and also mtu could 741 * be less than the receiver's page size ? 742 */ 743 int newlen; 744 struct mbuf *m; 745 746 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 747 off += m->m_len; 748 749 /* 750 * firstlen (off - hlen) must be aligned on an 751 * 8-byte boundary 752 */ 753 if (off < hlen) 754 goto smart_frag_failure; 755 off = ((off - hlen) & ~7) + hlen; 756 newlen = (~PAGE_MASK) & mtu; 757 if ((newlen + sizeof (struct ip)) > mtu) { 758 /* we failed, go back the default */ 759 smart_frag_failure: 760 newlen = len; 761 off = hlen + len; 762 } 763 len = newlen; 764 765 } else { 766 off = hlen + len; 767 } 768 769 firstlen = off - hlen; 770 mnext = &m0->m_nextpkt; /* pointer to next packet */ 771 772 /* 773 * Loop through length of segment after first fragment, 774 * make new header and copy data of each part and link onto chain. 775 * Here, m0 is the original packet, m is the fragment being created. 776 * The fragments are linked off the m_nextpkt of the original 777 * packet, which after processing serves as the first fragment. 778 */ 779 for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) { 780 struct ip *mhip; /* ip header on the fragment */ 781 struct mbuf *m; 782 int mhlen = sizeof (struct ip); 783 784 MGETHDR(m, M_DONTWAIT, MT_DATA); 785 if (m == NULL) { 786 error = ENOBUFS; 787 IPSTAT_INC(ips_odropped); 788 goto done; 789 } 790 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 791 /* 792 * In the first mbuf, leave room for the link header, then 793 * copy the original IP header including options. The payload 794 * goes into an additional mbuf chain returned by m_copym(). 795 */ 796 m->m_data += max_linkhdr; 797 mhip = mtod(m, struct ip *); 798 *mhip = *ip; 799 if (hlen > sizeof (struct ip)) { 800 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 801 mhip->ip_v = IPVERSION; 802 mhip->ip_hl = mhlen >> 2; 803 } 804 m->m_len = mhlen; 805 /* XXX do we need to add ip->ip_off below ? */ 806 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 807 if (off + len >= ip->ip_len) { /* last fragment */ 808 len = ip->ip_len - off; 809 m->m_flags |= M_LASTFRAG; 810 } else 811 mhip->ip_off |= IP_MF; 812 mhip->ip_len = htons((u_short)(len + mhlen)); 813 m->m_next = m_copym(m0, off, len, M_DONTWAIT); 814 if (m->m_next == NULL) { /* copy failed */ 815 m_free(m); 816 error = ENOBUFS; /* ??? */ 817 IPSTAT_INC(ips_odropped); 818 goto done; 819 } 820 m->m_pkthdr.len = mhlen + len; 821 m->m_pkthdr.rcvif = NULL; 822 #ifdef MAC 823 mac_netinet_fragment(m0, m); 824 #endif 825 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 826 mhip->ip_off = htons(mhip->ip_off); 827 mhip->ip_sum = 0; 828 if (sw_csum & CSUM_DELAY_IP) 829 mhip->ip_sum = in_cksum(m, mhlen); 830 *mnext = m; 831 mnext = &m->m_nextpkt; 832 } 833 IPSTAT_ADD(ips_ofragments, nfrags); 834 835 /* set first marker for fragment chain */ 836 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 837 m0->m_pkthdr.csum_data = nfrags; 838 839 /* 840 * Update first fragment by trimming what's been copied out 841 * and updating header. 842 */ 843 m_adj(m0, hlen + firstlen - ip->ip_len); 844 m0->m_pkthdr.len = hlen + firstlen; 845 ip->ip_len = htons((u_short)m0->m_pkthdr.len); 846 ip->ip_off |= IP_MF; 847 ip->ip_off = htons(ip->ip_off); 848 ip->ip_sum = 0; 849 if (sw_csum & CSUM_DELAY_IP) 850 ip->ip_sum = in_cksum(m0, hlen); 851 852 done: 853 *m_frag = m0; 854 return error; 855 } 856 857 void 858 in_delayed_cksum(struct mbuf *m) 859 { 860 struct ip *ip; 861 u_short csum, offset; 862 863 ip = mtod(m, struct ip *); 864 offset = ip->ip_hl << 2 ; 865 csum = in_cksum_skip(m, ip->ip_len, offset); 866 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 867 csum = 0xffff; 868 offset += m->m_pkthdr.csum_data; /* checksum offset */ 869 870 if (offset + sizeof(u_short) > m->m_len) { 871 printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 872 m->m_len, offset, ip->ip_p); 873 /* 874 * XXX 875 * this shouldn't happen, but if it does, the 876 * correct behavior may be to insert the checksum 877 * in the appropriate next mbuf in the chain. 878 */ 879 return; 880 } 881 *(u_short *)(m->m_data + offset) = csum; 882 } 883 884 /* 885 * IP socket option processing. 886 */ 887 int 888 ip_ctloutput(struct socket *so, struct sockopt *sopt) 889 { 890 struct inpcb *inp = sotoinpcb(so); 891 int error, optval; 892 893 error = optval = 0; 894 if (sopt->sopt_level != IPPROTO_IP) { 895 if ((sopt->sopt_level == SOL_SOCKET) && 896 (sopt->sopt_name == SO_SETFIB)) { 897 inp->inp_inc.inc_fibnum = so->so_fibnum; 898 return (0); 899 } 900 return (EINVAL); 901 } 902 903 switch (sopt->sopt_dir) { 904 case SOPT_SET: 905 switch (sopt->sopt_name) { 906 case IP_OPTIONS: 907 #ifdef notyet 908 case IP_RETOPTS: 909 #endif 910 { 911 struct mbuf *m; 912 if (sopt->sopt_valsize > MLEN) { 913 error = EMSGSIZE; 914 break; 915 } 916 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA); 917 if (m == NULL) { 918 error = ENOBUFS; 919 break; 920 } 921 m->m_len = sopt->sopt_valsize; 922 error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 923 m->m_len); 924 if (error) { 925 m_free(m); 926 break; 927 } 928 INP_WLOCK(inp); 929 error = ip_pcbopts(inp, sopt->sopt_name, m); 930 INP_WUNLOCK(inp); 931 return (error); 932 } 933 934 case IP_BINDANY: 935 if (sopt->sopt_td != NULL) { 936 error = priv_check(sopt->sopt_td, 937 PRIV_NETINET_BINDANY); 938 if (error) 939 break; 940 } 941 /* FALLTHROUGH */ 942 case IP_TOS: 943 case IP_TTL: 944 case IP_MINTTL: 945 case IP_RECVOPTS: 946 case IP_RECVRETOPTS: 947 case IP_RECVDSTADDR: 948 case IP_RECVTTL: 949 case IP_RECVIF: 950 case IP_FAITH: 951 case IP_ONESBCAST: 952 case IP_DONTFRAG: 953 error = sooptcopyin(sopt, &optval, sizeof optval, 954 sizeof optval); 955 if (error) 956 break; 957 958 switch (sopt->sopt_name) { 959 case IP_TOS: 960 inp->inp_ip_tos = optval; 961 break; 962 963 case IP_TTL: 964 inp->inp_ip_ttl = optval; 965 break; 966 967 case IP_MINTTL: 968 if (optval >= 0 && optval <= MAXTTL) 969 inp->inp_ip_minttl = optval; 970 else 971 error = EINVAL; 972 break; 973 974 #define OPTSET(bit) do { \ 975 INP_WLOCK(inp); \ 976 if (optval) \ 977 inp->inp_flags |= bit; \ 978 else \ 979 inp->inp_flags &= ~bit; \ 980 INP_WUNLOCK(inp); \ 981 } while (0) 982 983 case IP_RECVOPTS: 984 OPTSET(INP_RECVOPTS); 985 break; 986 987 case IP_RECVRETOPTS: 988 OPTSET(INP_RECVRETOPTS); 989 break; 990 991 case IP_RECVDSTADDR: 992 OPTSET(INP_RECVDSTADDR); 993 break; 994 995 case IP_RECVTTL: 996 OPTSET(INP_RECVTTL); 997 break; 998 999 case IP_RECVIF: 1000 OPTSET(INP_RECVIF); 1001 break; 1002 1003 case IP_FAITH: 1004 OPTSET(INP_FAITH); 1005 break; 1006 1007 case IP_ONESBCAST: 1008 OPTSET(INP_ONESBCAST); 1009 break; 1010 case IP_DONTFRAG: 1011 OPTSET(INP_DONTFRAG); 1012 break; 1013 case IP_BINDANY: 1014 OPTSET(INP_BINDANY); 1015 break; 1016 } 1017 break; 1018 #undef OPTSET 1019 1020 /* 1021 * Multicast socket options are processed by the in_mcast 1022 * module. 1023 */ 1024 case IP_MULTICAST_IF: 1025 case IP_MULTICAST_VIF: 1026 case IP_MULTICAST_TTL: 1027 case IP_MULTICAST_LOOP: 1028 case IP_ADD_MEMBERSHIP: 1029 case IP_DROP_MEMBERSHIP: 1030 case IP_ADD_SOURCE_MEMBERSHIP: 1031 case IP_DROP_SOURCE_MEMBERSHIP: 1032 case IP_BLOCK_SOURCE: 1033 case IP_UNBLOCK_SOURCE: 1034 case IP_MSFILTER: 1035 case MCAST_JOIN_GROUP: 1036 case MCAST_LEAVE_GROUP: 1037 case MCAST_JOIN_SOURCE_GROUP: 1038 case MCAST_LEAVE_SOURCE_GROUP: 1039 case MCAST_BLOCK_SOURCE: 1040 case MCAST_UNBLOCK_SOURCE: 1041 error = inp_setmoptions(inp, sopt); 1042 break; 1043 1044 case IP_PORTRANGE: 1045 error = sooptcopyin(sopt, &optval, sizeof optval, 1046 sizeof optval); 1047 if (error) 1048 break; 1049 1050 INP_WLOCK(inp); 1051 switch (optval) { 1052 case IP_PORTRANGE_DEFAULT: 1053 inp->inp_flags &= ~(INP_LOWPORT); 1054 inp->inp_flags &= ~(INP_HIGHPORT); 1055 break; 1056 1057 case IP_PORTRANGE_HIGH: 1058 inp->inp_flags &= ~(INP_LOWPORT); 1059 inp->inp_flags |= INP_HIGHPORT; 1060 break; 1061 1062 case IP_PORTRANGE_LOW: 1063 inp->inp_flags &= ~(INP_HIGHPORT); 1064 inp->inp_flags |= INP_LOWPORT; 1065 break; 1066 1067 default: 1068 error = EINVAL; 1069 break; 1070 } 1071 INP_WUNLOCK(inp); 1072 break; 1073 1074 #ifdef IPSEC 1075 case IP_IPSEC_POLICY: 1076 { 1077 caddr_t req; 1078 struct mbuf *m; 1079 1080 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1081 break; 1082 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1083 break; 1084 req = mtod(m, caddr_t); 1085 error = ipsec_set_policy(inp, sopt->sopt_name, req, 1086 m->m_len, (sopt->sopt_td != NULL) ? 1087 sopt->sopt_td->td_ucred : NULL); 1088 m_freem(m); 1089 break; 1090 } 1091 #endif /* IPSEC */ 1092 1093 default: 1094 error = ENOPROTOOPT; 1095 break; 1096 } 1097 break; 1098 1099 case SOPT_GET: 1100 switch (sopt->sopt_name) { 1101 case IP_OPTIONS: 1102 case IP_RETOPTS: 1103 if (inp->inp_options) 1104 error = sooptcopyout(sopt, 1105 mtod(inp->inp_options, 1106 char *), 1107 inp->inp_options->m_len); 1108 else 1109 sopt->sopt_valsize = 0; 1110 break; 1111 1112 case IP_TOS: 1113 case IP_TTL: 1114 case IP_MINTTL: 1115 case IP_RECVOPTS: 1116 case IP_RECVRETOPTS: 1117 case IP_RECVDSTADDR: 1118 case IP_RECVTTL: 1119 case IP_RECVIF: 1120 case IP_PORTRANGE: 1121 case IP_FAITH: 1122 case IP_ONESBCAST: 1123 case IP_DONTFRAG: 1124 switch (sopt->sopt_name) { 1125 1126 case IP_TOS: 1127 optval = inp->inp_ip_tos; 1128 break; 1129 1130 case IP_TTL: 1131 optval = inp->inp_ip_ttl; 1132 break; 1133 1134 case IP_MINTTL: 1135 optval = inp->inp_ip_minttl; 1136 break; 1137 1138 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1139 1140 case IP_RECVOPTS: 1141 optval = OPTBIT(INP_RECVOPTS); 1142 break; 1143 1144 case IP_RECVRETOPTS: 1145 optval = OPTBIT(INP_RECVRETOPTS); 1146 break; 1147 1148 case IP_RECVDSTADDR: 1149 optval = OPTBIT(INP_RECVDSTADDR); 1150 break; 1151 1152 case IP_RECVTTL: 1153 optval = OPTBIT(INP_RECVTTL); 1154 break; 1155 1156 case IP_RECVIF: 1157 optval = OPTBIT(INP_RECVIF); 1158 break; 1159 1160 case IP_PORTRANGE: 1161 if (inp->inp_flags & INP_HIGHPORT) 1162 optval = IP_PORTRANGE_HIGH; 1163 else if (inp->inp_flags & INP_LOWPORT) 1164 optval = IP_PORTRANGE_LOW; 1165 else 1166 optval = 0; 1167 break; 1168 1169 case IP_FAITH: 1170 optval = OPTBIT(INP_FAITH); 1171 break; 1172 1173 case IP_ONESBCAST: 1174 optval = OPTBIT(INP_ONESBCAST); 1175 break; 1176 case IP_DONTFRAG: 1177 optval = OPTBIT(INP_DONTFRAG); 1178 break; 1179 } 1180 error = sooptcopyout(sopt, &optval, sizeof optval); 1181 break; 1182 1183 /* 1184 * Multicast socket options are processed by the in_mcast 1185 * module. 1186 */ 1187 case IP_MULTICAST_IF: 1188 case IP_MULTICAST_VIF: 1189 case IP_MULTICAST_TTL: 1190 case IP_MULTICAST_LOOP: 1191 case IP_MSFILTER: 1192 error = inp_getmoptions(inp, sopt); 1193 break; 1194 1195 #ifdef IPSEC 1196 case IP_IPSEC_POLICY: 1197 { 1198 struct mbuf *m = NULL; 1199 caddr_t req = NULL; 1200 size_t len = 0; 1201 1202 if (m != 0) { 1203 req = mtod(m, caddr_t); 1204 len = m->m_len; 1205 } 1206 error = ipsec_get_policy(sotoinpcb(so), req, len, &m); 1207 if (error == 0) 1208 error = soopt_mcopyout(sopt, m); /* XXX */ 1209 if (error == 0) 1210 m_freem(m); 1211 break; 1212 } 1213 #endif /* IPSEC */ 1214 1215 default: 1216 error = ENOPROTOOPT; 1217 break; 1218 } 1219 break; 1220 } 1221 return (error); 1222 } 1223 1224 /* 1225 * Routine called from ip_output() to loop back a copy of an IP multicast 1226 * packet to the input queue of a specified interface. Note that this 1227 * calls the output routine of the loopback "driver", but with an interface 1228 * pointer that might NOT be a loopback interface -- evil, but easier than 1229 * replicating that code here. 1230 */ 1231 static void 1232 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 1233 int hlen) 1234 { 1235 register struct ip *ip; 1236 struct mbuf *copym; 1237 1238 /* 1239 * Make a deep copy of the packet because we're going to 1240 * modify the pack in order to generate checksums. 1241 */ 1242 copym = m_dup(m, M_DONTWAIT); 1243 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 1244 copym = m_pullup(copym, hlen); 1245 if (copym != NULL) { 1246 /* If needed, compute the checksum and mark it as valid. */ 1247 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1248 in_delayed_cksum(copym); 1249 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1250 copym->m_pkthdr.csum_flags |= 1251 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1252 copym->m_pkthdr.csum_data = 0xffff; 1253 } 1254 /* 1255 * We don't bother to fragment if the IP length is greater 1256 * than the interface's MTU. Can this possibly matter? 1257 */ 1258 ip = mtod(copym, struct ip *); 1259 ip->ip_len = htons(ip->ip_len); 1260 ip->ip_off = htons(ip->ip_off); 1261 ip->ip_sum = 0; 1262 ip->ip_sum = in_cksum(copym, hlen); 1263 #if 1 /* XXX */ 1264 if (dst->sin_family != AF_INET) { 1265 printf("ip_mloopback: bad address family %d\n", 1266 dst->sin_family); 1267 dst->sin_family = AF_INET; 1268 } 1269 #endif 1270 if_simloop(ifp, copym, dst->sin_family, 0); 1271 } 1272 } 1273