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