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