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_ratelimit.h" 42 #include "opt_route.h" 43 #include "opt_rss.h" 44 #include "opt_sctp.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/ktls.h> 50 #include <sys/lock.h> 51 #include <sys/malloc.h> 52 #include <sys/mbuf.h> 53 #include <sys/priv.h> 54 #include <sys/proc.h> 55 #include <sys/protosw.h> 56 #include <sys/sdt.h> 57 #include <sys/socket.h> 58 #include <sys/socketvar.h> 59 #include <sys/sysctl.h> 60 #include <sys/ucred.h> 61 62 #include <net/if.h> 63 #include <net/if_var.h> 64 #include <net/if_vlan_var.h> 65 #include <net/if_llatbl.h> 66 #include <net/ethernet.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 #if defined(SCTP) || defined(SCTP_SUPPORT) 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 extern int in_mcast_loop; 109 110 static inline int 111 ip_output_pfil(struct mbuf **mp, struct ifnet *ifp, int flags, 112 struct inpcb *inp, struct sockaddr_in *dst, int *fibnum, int *error) 113 { 114 struct m_tag *fwd_tag = NULL; 115 struct mbuf *m; 116 struct in_addr odst; 117 struct ip *ip; 118 int pflags = PFIL_OUT; 119 120 if (flags & IP_FORWARDING) 121 pflags |= PFIL_FWD; 122 123 m = *mp; 124 ip = mtod(m, struct ip *); 125 126 /* Run through list of hooks for output packets. */ 127 odst.s_addr = ip->ip_dst.s_addr; 128 switch (pfil_run_hooks(V_inet_pfil_head, mp, ifp, pflags, inp)) { 129 case PFIL_DROPPED: 130 *error = EACCES; 131 /* FALLTHROUGH */ 132 case PFIL_CONSUMED: 133 return 1; /* Finished */ 134 case PFIL_PASS: 135 *error = 0; 136 } 137 m = *mp; 138 ip = mtod(m, struct ip *); 139 140 /* See if destination IP address was changed by packet filter. */ 141 if (odst.s_addr != ip->ip_dst.s_addr) { 142 m->m_flags |= M_SKIP_FIREWALL; 143 /* If destination is now ourself drop to ip_input(). */ 144 if (in_localip(ip->ip_dst)) { 145 m->m_flags |= M_FASTFWD_OURS; 146 if (m->m_pkthdr.rcvif == NULL) 147 m->m_pkthdr.rcvif = V_loif; 148 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 149 m->m_pkthdr.csum_flags |= 150 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 151 m->m_pkthdr.csum_data = 0xffff; 152 } 153 m->m_pkthdr.csum_flags |= 154 CSUM_IP_CHECKED | CSUM_IP_VALID; 155 #if defined(SCTP) || defined(SCTP_SUPPORT) 156 if (m->m_pkthdr.csum_flags & CSUM_SCTP) 157 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 158 #endif 159 *error = netisr_queue(NETISR_IP, m); 160 return 1; /* Finished */ 161 } 162 163 bzero(dst, sizeof(*dst)); 164 dst->sin_family = AF_INET; 165 dst->sin_len = sizeof(*dst); 166 dst->sin_addr = ip->ip_dst; 167 168 return -1; /* Reloop */ 169 } 170 /* See if fib was changed by packet filter. */ 171 if ((*fibnum) != M_GETFIB(m)) { 172 m->m_flags |= M_SKIP_FIREWALL; 173 *fibnum = M_GETFIB(m); 174 return -1; /* Reloop for FIB change */ 175 } 176 177 /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 178 if (m->m_flags & M_FASTFWD_OURS) { 179 if (m->m_pkthdr.rcvif == NULL) 180 m->m_pkthdr.rcvif = V_loif; 181 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 182 m->m_pkthdr.csum_flags |= 183 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 184 m->m_pkthdr.csum_data = 0xffff; 185 } 186 #if defined(SCTP) || defined(SCTP_SUPPORT) 187 if (m->m_pkthdr.csum_flags & CSUM_SCTP) 188 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 189 #endif 190 m->m_pkthdr.csum_flags |= 191 CSUM_IP_CHECKED | CSUM_IP_VALID; 192 193 *error = netisr_queue(NETISR_IP, m); 194 return 1; /* Finished */ 195 } 196 /* Or forward to some other address? */ 197 if ((m->m_flags & M_IP_NEXTHOP) && 198 ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) { 199 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 200 m->m_flags |= M_SKIP_FIREWALL; 201 m->m_flags &= ~M_IP_NEXTHOP; 202 m_tag_delete(m, fwd_tag); 203 204 return -1; /* Reloop for CHANGE of dst */ 205 } 206 207 return 0; 208 } 209 210 static int 211 ip_output_send(struct inpcb *inp, struct ifnet *ifp, struct mbuf *m, 212 const struct sockaddr *gw, struct route *ro, bool stamp_tag) 213 { 214 #ifdef KERN_TLS 215 struct ktls_session *tls = NULL; 216 #endif 217 struct m_snd_tag *mst; 218 int error; 219 220 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 221 mst = NULL; 222 223 #ifdef KERN_TLS 224 /* 225 * If this is an unencrypted TLS record, save a reference to 226 * the record. This local reference is used to call 227 * ktls_output_eagain after the mbuf has been freed (thus 228 * dropping the mbuf's reference) in if_output. 229 */ 230 if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) { 231 tls = ktls_hold(m->m_next->m_epg_tls); 232 mst = tls->snd_tag; 233 234 /* 235 * If a TLS session doesn't have a valid tag, it must 236 * have had an earlier ifp mismatch, so drop this 237 * packet. 238 */ 239 if (mst == NULL) { 240 m_freem(m); 241 error = EAGAIN; 242 goto done; 243 } 244 /* 245 * Always stamp tags that include NIC ktls. 246 */ 247 stamp_tag = true; 248 } 249 #endif 250 #ifdef RATELIMIT 251 if (inp != NULL && mst == NULL) { 252 if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 || 253 (inp->inp_snd_tag != NULL && 254 inp->inp_snd_tag->ifp != ifp)) 255 in_pcboutput_txrtlmt(inp, ifp, m); 256 257 if (inp->inp_snd_tag != NULL) 258 mst = inp->inp_snd_tag; 259 } 260 #endif 261 if (stamp_tag && mst != NULL) { 262 KASSERT(m->m_pkthdr.rcvif == NULL, 263 ("trying to add a send tag to a forwarded packet")); 264 if (mst->ifp != ifp) { 265 m_freem(m); 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, 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, const struct nhop_object *nh) 296 { 297 int nh_flags = 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 ip *ip; 323 struct ifnet *ifp = NULL; /* keep compiler happy */ 324 struct mbuf *m0; 325 int hlen = sizeof (struct ip); 326 int mtu = 0; 327 int error = 0; 328 int vlan_pcp = -1; 329 struct sockaddr_in *dst; 330 const struct sockaddr *gw; 331 struct in_ifaddr *ia = NULL; 332 struct in_addr src; 333 int isbroadcast; 334 uint16_t ip_len, ip_off; 335 struct route iproute; 336 uint32_t fibnum; 337 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 338 int no_route_but_check_spd = 0; 339 #endif 340 341 M_ASSERTPKTHDR(m); 342 NET_EPOCH_ASSERT(); 343 344 if (inp != NULL) { 345 INP_LOCK_ASSERT(inp); 346 M_SETFIB(m, inp->inp_inc.inc_fibnum); 347 if ((flags & IP_NODEFAULTFLOWID) == 0) { 348 m->m_pkthdr.flowid = inp->inp_flowid; 349 M_HASHTYPE_SET(m, inp->inp_flowtype); 350 } 351 if ((inp->inp_flags2 & INP_2PCP_SET) != 0) 352 vlan_pcp = (inp->inp_flags2 & INP_2PCP_MASK) >> 353 INP_2PCP_SHIFT; 354 #ifdef NUMA 355 m->m_pkthdr.numa_domain = inp->inp_numa_domain; 356 #endif 357 } 358 359 if (opt) { 360 int len = 0; 361 m = ip_insertoptions(m, opt, &len); 362 if (len != 0) 363 hlen = len; /* ip->ip_hl is updated above */ 364 } 365 ip = mtod(m, struct ip *); 366 ip_len = ntohs(ip->ip_len); 367 ip_off = ntohs(ip->ip_off); 368 369 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 370 ip->ip_v = IPVERSION; 371 ip->ip_hl = hlen >> 2; 372 ip_fillid(ip); 373 } else { 374 /* Header already set, fetch hlen from there */ 375 hlen = ip->ip_hl << 2; 376 } 377 if ((flags & IP_FORWARDING) == 0) 378 IPSTAT_INC(ips_localout); 379 380 /* 381 * dst/gw handling: 382 * 383 * gw is readonly but can point either to dst OR rt_gateway, 384 * therefore we need restore gw if we're redoing lookup. 385 */ 386 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m); 387 if (ro == NULL) { 388 ro = &iproute; 389 bzero(ro, sizeof (*ro)); 390 } 391 dst = (struct sockaddr_in *)&ro->ro_dst; 392 if (ro->ro_nh == NULL) { 393 dst->sin_family = AF_INET; 394 dst->sin_len = sizeof(*dst); 395 dst->sin_addr = ip->ip_dst; 396 } 397 gw = (const struct sockaddr *)dst; 398 again: 399 /* 400 * Validate route against routing table additions; 401 * a better/more specific route might have been added. 402 */ 403 if (inp != NULL && ro->ro_nh != NULL) 404 NH_VALIDATE(ro, &inp->inp_rt_cookie, fibnum); 405 /* 406 * If there is a cached route, 407 * check that it is to the same destination 408 * and is still up. If not, free it and try again. 409 * The address family should also be checked in case of sharing the 410 * cache with IPv6. 411 * Also check whether routing cache needs invalidation. 412 */ 413 if (ro->ro_nh != NULL && 414 ((!NH_IS_VALID(ro->ro_nh)) || dst->sin_family != AF_INET || 415 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) 416 RO_INVALIDATE_CACHE(ro); 417 ia = NULL; 418 /* 419 * If routing to interface only, short circuit routing lookup. 420 * The use of an all-ones broadcast address implies this; an 421 * interface is specified by the broadcast address of an interface, 422 * or the destination address of a ptp interface. 423 */ 424 if (flags & IP_SENDONES) { 425 if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst), 426 M_GETFIB(m)))) == NULL && 427 (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst), 428 M_GETFIB(m)))) == NULL) { 429 IPSTAT_INC(ips_noroute); 430 error = ENETUNREACH; 431 goto bad; 432 } 433 ip->ip_dst.s_addr = INADDR_BROADCAST; 434 dst->sin_addr = ip->ip_dst; 435 ifp = ia->ia_ifp; 436 mtu = ifp->if_mtu; 437 ip->ip_ttl = 1; 438 isbroadcast = 1; 439 src = IA_SIN(ia)->sin_addr; 440 } else if (flags & IP_ROUTETOIF) { 441 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst), 442 M_GETFIB(m)))) == NULL && 443 (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0, 444 M_GETFIB(m)))) == NULL) { 445 IPSTAT_INC(ips_noroute); 446 error = ENETUNREACH; 447 goto bad; 448 } 449 ifp = ia->ia_ifp; 450 mtu = ifp->if_mtu; 451 ip->ip_ttl = 1; 452 isbroadcast = ifp->if_flags & IFF_BROADCAST ? 453 in_ifaddr_broadcast(dst->sin_addr, ia) : 0; 454 src = IA_SIN(ia)->sin_addr; 455 } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 456 imo != NULL && imo->imo_multicast_ifp != NULL) { 457 /* 458 * Bypass the normal routing lookup for multicast 459 * packets if the interface is specified. 460 */ 461 ifp = imo->imo_multicast_ifp; 462 mtu = ifp->if_mtu; 463 IFP_TO_IA(ifp, ia); 464 isbroadcast = 0; /* fool gcc */ 465 /* Interface may have no addresses. */ 466 if (ia != NULL) 467 src = IA_SIN(ia)->sin_addr; 468 else 469 src.s_addr = INADDR_ANY; 470 } else if (ro != &iproute) { 471 if (ro->ro_nh == NULL) { 472 /* 473 * We want to do any cloning requested by the link 474 * layer, as this is probably required in all cases 475 * for correct operation (as it is for ARP). 476 */ 477 uint32_t flowid; 478 flowid = m->m_pkthdr.flowid; 479 ro->ro_nh = fib4_lookup(fibnum, dst->sin_addr, 0, 480 NHR_REF, flowid); 481 482 if (ro->ro_nh == NULL || (!NH_IS_VALID(ro->ro_nh))) { 483 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 484 /* 485 * There is no route for this packet, but it is 486 * possible that a matching SPD entry exists. 487 */ 488 no_route_but_check_spd = 1; 489 goto sendit; 490 #endif 491 IPSTAT_INC(ips_noroute); 492 error = EHOSTUNREACH; 493 goto bad; 494 } 495 } 496 struct nhop_object *nh = ro->ro_nh; 497 498 ia = ifatoia(nh->nh_ifa); 499 ifp = nh->nh_ifp; 500 counter_u64_add(nh->nh_pksent, 1); 501 rt_update_ro_flags(ro, nh); 502 if (nh->nh_flags & NHF_GATEWAY) 503 gw = &nh->gw_sa; 504 if (nh->nh_flags & NHF_HOST) 505 isbroadcast = (nh->nh_flags & NHF_BROADCAST); 506 else if ((ifp->if_flags & IFF_BROADCAST) && (gw->sa_family == AF_INET)) 507 isbroadcast = in_ifaddr_broadcast(((const struct sockaddr_in *)gw)->sin_addr, ia); 508 else 509 isbroadcast = 0; 510 mtu = nh->nh_mtu; 511 src = IA_SIN(ia)->sin_addr; 512 } else { 513 struct nhop_object *nh; 514 515 nh = fib4_lookup(M_GETFIB(m), dst->sin_addr, 0, NHR_NONE, 516 m->m_pkthdr.flowid); 517 if (nh == NULL) { 518 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 519 /* 520 * There is no route for this packet, but it is 521 * possible that a matching SPD entry exists. 522 */ 523 no_route_but_check_spd = 1; 524 goto sendit; 525 #endif 526 IPSTAT_INC(ips_noroute); 527 error = EHOSTUNREACH; 528 goto bad; 529 } 530 ifp = nh->nh_ifp; 531 mtu = nh->nh_mtu; 532 rt_update_ro_flags(ro, nh); 533 if (nh->nh_flags & NHF_GATEWAY) 534 gw = &nh->gw_sa; 535 ia = ifatoia(nh->nh_ifa); 536 src = IA_SIN(ia)->sin_addr; 537 isbroadcast = (((nh->nh_flags & (NHF_HOST | NHF_BROADCAST)) == 538 (NHF_HOST | NHF_BROADCAST)) || 539 ((ifp->if_flags & IFF_BROADCAST) && 540 (gw->sa_family == AF_INET) && 541 in_ifaddr_broadcast(((const struct sockaddr_in *)gw)->sin_addr, ia))); 542 } 543 544 /* Catch a possible divide by zero later. */ 545 KASSERT(mtu > 0, ("%s: mtu %d <= 0, ro=%p (nh_flags=0x%08x) ifp=%p", 546 __func__, mtu, ro, 547 (ro != NULL && ro->ro_nh != NULL) ? ro->ro_nh->nh_flags : 0, ifp)); 548 549 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 550 m->m_flags |= M_MCAST; 551 /* 552 * IP destination address is multicast. Make sure "gw" 553 * still points to the address in "ro". (It may have been 554 * changed to point to a gateway address, above.) 555 */ 556 gw = (const struct sockaddr *)dst; 557 /* 558 * See if the caller provided any multicast options 559 */ 560 if (imo != NULL) { 561 ip->ip_ttl = imo->imo_multicast_ttl; 562 if (imo->imo_multicast_vif != -1) 563 ip->ip_src.s_addr = 564 ip_mcast_src ? 565 ip_mcast_src(imo->imo_multicast_vif) : 566 INADDR_ANY; 567 } else 568 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 569 /* 570 * Confirm that the outgoing interface supports multicast. 571 */ 572 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 573 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 574 IPSTAT_INC(ips_noroute); 575 error = ENETUNREACH; 576 goto bad; 577 } 578 } 579 /* 580 * If source address not specified yet, use address 581 * of outgoing interface. 582 */ 583 if (ip->ip_src.s_addr == INADDR_ANY) 584 ip->ip_src = src; 585 586 if ((imo == NULL && in_mcast_loop) || 587 (imo && imo->imo_multicast_loop)) { 588 /* 589 * Loop back multicast datagram if not expressly 590 * forbidden to do so, even if we are not a member 591 * of the group; ip_input() will filter it later, 592 * thus deferring a hash lookup and mutex acquisition 593 * at the expense of a cheap copy using m_copym(). 594 */ 595 ip_mloopback(ifp, m, hlen); 596 } else { 597 /* 598 * If we are acting as a multicast router, perform 599 * multicast forwarding as if the packet had just 600 * arrived on the interface to which we are about 601 * to send. The multicast forwarding function 602 * recursively calls this function, using the 603 * IP_FORWARDING flag to prevent infinite recursion. 604 * 605 * Multicasts that are looped back by ip_mloopback(), 606 * above, will be forwarded by the ip_input() routine, 607 * if necessary. 608 */ 609 if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) { 610 /* 611 * If rsvp daemon is not running, do not 612 * set ip_moptions. This ensures that the packet 613 * is multicast and not just sent down one link 614 * as prescribed by rsvpd. 615 */ 616 if (!V_rsvp_on) 617 imo = NULL; 618 if (ip_mforward && 619 ip_mforward(ip, ifp, m, imo) != 0) { 620 m_freem(m); 621 goto done; 622 } 623 } 624 } 625 626 /* 627 * Multicasts with a time-to-live of zero may be looped- 628 * back, above, but must not be transmitted on a network. 629 * Also, multicasts addressed to the loopback interface 630 * are not sent -- the above call to ip_mloopback() will 631 * loop back a copy. ip_input() will drop the copy if 632 * this host does not belong to the destination group on 633 * the loopback interface. 634 */ 635 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 636 m_freem(m); 637 goto done; 638 } 639 640 goto sendit; 641 } 642 643 /* 644 * If the source address is not specified yet, use the address 645 * of the outoing interface. 646 */ 647 if (ip->ip_src.s_addr == INADDR_ANY) 648 ip->ip_src = src; 649 650 /* 651 * Look for broadcast address and 652 * verify user is allowed to send 653 * such a packet. 654 */ 655 if (isbroadcast) { 656 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 657 error = EADDRNOTAVAIL; 658 goto bad; 659 } 660 if ((flags & IP_ALLOWBROADCAST) == 0) { 661 error = EACCES; 662 goto bad; 663 } 664 /* don't allow broadcast messages to be fragmented */ 665 if (ip_len > mtu) { 666 error = EMSGSIZE; 667 goto bad; 668 } 669 m->m_flags |= M_BCAST; 670 } else { 671 m->m_flags &= ~M_BCAST; 672 } 673 674 sendit: 675 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 676 if (IPSEC_ENABLED(ipv4)) { 677 if ((error = IPSEC_OUTPUT(ipv4, m, inp)) != 0) { 678 if (error == EINPROGRESS) 679 error = 0; 680 goto done; 681 } 682 } 683 /* 684 * Check if there was a route for this packet; return error if not. 685 */ 686 if (no_route_but_check_spd) { 687 IPSTAT_INC(ips_noroute); 688 error = EHOSTUNREACH; 689 goto bad; 690 } 691 /* Update variables that are affected by ipsec4_output(). */ 692 ip = mtod(m, struct ip *); 693 hlen = ip->ip_hl << 2; 694 #endif /* IPSEC */ 695 696 /* Jump over all PFIL processing if hooks are not active. */ 697 if (PFIL_HOOKED_OUT(V_inet_pfil_head)) { 698 switch (ip_output_pfil(&m, ifp, flags, inp, dst, &fibnum, 699 &error)) { 700 case 1: /* Finished */ 701 goto done; 702 703 case 0: /* Continue normally */ 704 ip = mtod(m, struct ip *); 705 break; 706 707 case -1: /* Need to try again */ 708 /* Reset everything for a new round */ 709 if (ro != NULL) { 710 RO_NHFREE(ro); 711 ro->ro_prepend = NULL; 712 } 713 gw = (const struct sockaddr *)dst; 714 ip = mtod(m, struct ip *); 715 goto again; 716 } 717 } 718 719 if (vlan_pcp > -1) 720 EVL_APPLY_PRI(m, vlan_pcp); 721 722 /* IN_LOOPBACK must not appear on the wire - RFC1122. */ 723 if (IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) || 724 IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) { 725 if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 726 IPSTAT_INC(ips_badaddr); 727 error = EADDRNOTAVAIL; 728 goto bad; 729 } 730 } 731 732 /* Ensure the packet data is mapped if the interface requires it. */ 733 if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) { 734 m = mb_unmapped_to_ext(m); 735 if (m == NULL) { 736 IPSTAT_INC(ips_odropped); 737 error = ENOBUFS; 738 goto bad; 739 } 740 } 741 742 m->m_pkthdr.csum_flags |= CSUM_IP; 743 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 744 in_delayed_cksum(m); 745 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 746 } 747 #if defined(SCTP) || defined(SCTP_SUPPORT) 748 if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 749 sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 750 m->m_pkthdr.csum_flags &= ~CSUM_SCTP; 751 } 752 #endif 753 754 /* 755 * If small enough for interface, or the interface will take 756 * care of the fragmentation for us, we can just send directly. 757 * Note that if_vxlan could have requested TSO even though the outer 758 * frame is UDP. It is correct to not fragment such datagrams and 759 * instead just pass them on to the driver. 760 */ 761 if (ip_len <= mtu || 762 (m->m_pkthdr.csum_flags & ifp->if_hwassist & 763 (CSUM_TSO | CSUM_INNER_TSO)) != 0) { 764 ip->ip_sum = 0; 765 if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) { 766 ip->ip_sum = in_cksum(m, hlen); 767 m->m_pkthdr.csum_flags &= ~CSUM_IP; 768 } 769 770 /* 771 * Record statistics for this interface address. 772 * With CSUM_TSO the byte/packet count will be slightly 773 * incorrect because we count the IP+TCP headers only 774 * once instead of for every generated packet. 775 */ 776 if (!(flags & IP_FORWARDING) && ia) { 777 if (m->m_pkthdr.csum_flags & 778 (CSUM_TSO | CSUM_INNER_TSO)) 779 counter_u64_add(ia->ia_ifa.ifa_opackets, 780 m->m_pkthdr.len / m->m_pkthdr.tso_segsz); 781 else 782 counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 783 784 counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len); 785 } 786 #ifdef MBUF_STRESS_TEST 787 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 788 m = m_fragment(m, M_NOWAIT, mbuf_frag_size); 789 #endif 790 /* 791 * Reset layer specific mbuf flags 792 * to avoid confusing lower layers. 793 */ 794 m_clrprotoflags(m); 795 IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 796 error = ip_output_send(inp, ifp, m, gw, ro, 797 (flags & IP_NO_SND_TAG_RL) ? false : true); 798 goto done; 799 } 800 801 /* Balk when DF bit is set or the interface didn't support TSO. */ 802 if ((ip_off & IP_DF) || 803 (m->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_INNER_TSO))) { 804 error = EMSGSIZE; 805 IPSTAT_INC(ips_cantfrag); 806 goto bad; 807 } 808 809 /* 810 * Too large for interface; fragment if possible. If successful, 811 * on return, m will point to a list of packets to be sent. 812 */ 813 error = ip_fragment(ip, &m, mtu, ifp->if_hwassist); 814 if (error) 815 goto bad; 816 for (; m; m = m0) { 817 m0 = m->m_nextpkt; 818 m->m_nextpkt = 0; 819 if (error == 0) { 820 /* Record statistics for this interface address. */ 821 if (ia != NULL) { 822 counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 823 counter_u64_add(ia->ia_ifa.ifa_obytes, 824 m->m_pkthdr.len); 825 } 826 /* 827 * Reset layer specific mbuf flags 828 * to avoid confusing upper layers. 829 */ 830 m_clrprotoflags(m); 831 832 IP_PROBE(send, NULL, NULL, mtod(m, struct ip *), ifp, 833 mtod(m, struct ip *), NULL); 834 error = ip_output_send(inp, ifp, m, gw, ro, true); 835 } else 836 m_freem(m); 837 } 838 839 if (error == 0) 840 IPSTAT_INC(ips_fragmented); 841 842 done: 843 return (error); 844 bad: 845 m_freem(m); 846 goto done; 847 } 848 849 /* 850 * Create a chain of fragments which fit the given mtu. m_frag points to the 851 * mbuf to be fragmented; on return it points to the chain with the fragments. 852 * Return 0 if no error. If error, m_frag may contain a partially built 853 * chain of fragments that should be freed by the caller. 854 * 855 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 856 */ 857 int 858 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 859 u_long if_hwassist_flags) 860 { 861 int error = 0; 862 int hlen = ip->ip_hl << 2; 863 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 864 int off; 865 struct mbuf *m0 = *m_frag; /* the original packet */ 866 int firstlen; 867 struct mbuf **mnext; 868 int nfrags; 869 uint16_t ip_len, ip_off; 870 871 ip_len = ntohs(ip->ip_len); 872 ip_off = ntohs(ip->ip_off); 873 874 if (ip_off & IP_DF) { /* Fragmentation not allowed */ 875 IPSTAT_INC(ips_cantfrag); 876 return EMSGSIZE; 877 } 878 879 /* 880 * Must be able to put at least 8 bytes per fragment. 881 */ 882 if (len < 8) 883 return EMSGSIZE; 884 885 /* 886 * If the interface will not calculate checksums on 887 * fragmented packets, then do it here. 888 */ 889 if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 890 in_delayed_cksum(m0); 891 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 892 } 893 #if defined(SCTP) || defined(SCTP_SUPPORT) 894 if (m0->m_pkthdr.csum_flags & CSUM_SCTP) { 895 sctp_delayed_cksum(m0, hlen); 896 m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 897 } 898 #endif 899 if (len > PAGE_SIZE) { 900 /* 901 * Fragment large datagrams such that each segment 902 * contains a multiple of PAGE_SIZE amount of data, 903 * plus headers. This enables a receiver to perform 904 * page-flipping zero-copy optimizations. 905 * 906 * XXX When does this help given that sender and receiver 907 * could have different page sizes, and also mtu could 908 * be less than the receiver's page size ? 909 */ 910 int newlen; 911 912 off = MIN(mtu, m0->m_pkthdr.len); 913 914 /* 915 * firstlen (off - hlen) must be aligned on an 916 * 8-byte boundary 917 */ 918 if (off < hlen) 919 goto smart_frag_failure; 920 off = ((off - hlen) & ~7) + hlen; 921 newlen = (~PAGE_MASK) & mtu; 922 if ((newlen + sizeof (struct ip)) > mtu) { 923 /* we failed, go back the default */ 924 smart_frag_failure: 925 newlen = len; 926 off = hlen + len; 927 } 928 len = newlen; 929 930 } else { 931 off = hlen + len; 932 } 933 934 firstlen = off - hlen; 935 mnext = &m0->m_nextpkt; /* pointer to next packet */ 936 937 /* 938 * Loop through length of segment after first fragment, 939 * make new header and copy data of each part and link onto chain. 940 * Here, m0 is the original packet, m is the fragment being created. 941 * The fragments are linked off the m_nextpkt of the original 942 * packet, which after processing serves as the first fragment. 943 */ 944 for (nfrags = 1; off < ip_len; off += len, nfrags++) { 945 struct ip *mhip; /* ip header on the fragment */ 946 struct mbuf *m; 947 int mhlen = sizeof (struct ip); 948 949 m = m_gethdr(M_NOWAIT, MT_DATA); 950 if (m == NULL) { 951 error = ENOBUFS; 952 IPSTAT_INC(ips_odropped); 953 goto done; 954 } 955 /* 956 * Make sure the complete packet header gets copied 957 * from the originating mbuf to the newly created 958 * mbuf. This also ensures that existing firewall 959 * classification(s), VLAN tags and so on get copied 960 * to the resulting fragmented packet(s): 961 */ 962 if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) { 963 m_free(m); 964 error = ENOBUFS; 965 IPSTAT_INC(ips_odropped); 966 goto done; 967 } 968 /* 969 * In the first mbuf, leave room for the link header, then 970 * copy the original IP header including options. The payload 971 * goes into an additional mbuf chain returned by m_copym(). 972 */ 973 m->m_data += max_linkhdr; 974 mhip = mtod(m, struct ip *); 975 *mhip = *ip; 976 if (hlen > sizeof (struct ip)) { 977 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 978 mhip->ip_v = IPVERSION; 979 mhip->ip_hl = mhlen >> 2; 980 } 981 m->m_len = mhlen; 982 /* XXX do we need to add ip_off below ? */ 983 mhip->ip_off = ((off - hlen) >> 3) + ip_off; 984 if (off + len >= ip_len) 985 len = ip_len - off; 986 else 987 mhip->ip_off |= IP_MF; 988 mhip->ip_len = htons((u_short)(len + mhlen)); 989 m->m_next = m_copym(m0, off, len, M_NOWAIT); 990 if (m->m_next == NULL) { /* copy failed */ 991 m_free(m); 992 error = ENOBUFS; /* ??? */ 993 IPSTAT_INC(ips_odropped); 994 goto done; 995 } 996 m->m_pkthdr.len = mhlen + len; 997 #ifdef MAC 998 mac_netinet_fragment(m0, m); 999 #endif 1000 mhip->ip_off = htons(mhip->ip_off); 1001 mhip->ip_sum = 0; 1002 if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 1003 mhip->ip_sum = in_cksum(m, mhlen); 1004 m->m_pkthdr.csum_flags &= ~CSUM_IP; 1005 } 1006 *mnext = m; 1007 mnext = &m->m_nextpkt; 1008 } 1009 IPSTAT_ADD(ips_ofragments, nfrags); 1010 1011 /* 1012 * Update first fragment by trimming what's been copied out 1013 * and updating header. 1014 */ 1015 m_adj(m0, hlen + firstlen - ip_len); 1016 m0->m_pkthdr.len = hlen + firstlen; 1017 ip->ip_len = htons((u_short)m0->m_pkthdr.len); 1018 ip->ip_off = htons(ip_off | IP_MF); 1019 ip->ip_sum = 0; 1020 if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 1021 ip->ip_sum = in_cksum(m0, hlen); 1022 m0->m_pkthdr.csum_flags &= ~CSUM_IP; 1023 } 1024 1025 done: 1026 *m_frag = m0; 1027 return error; 1028 } 1029 1030 void 1031 in_delayed_cksum(struct mbuf *m) 1032 { 1033 struct ip *ip; 1034 struct udphdr *uh; 1035 uint16_t cklen, csum, offset; 1036 1037 ip = mtod(m, struct ip *); 1038 offset = ip->ip_hl << 2 ; 1039 1040 if (m->m_pkthdr.csum_flags & CSUM_UDP) { 1041 /* if udp header is not in the first mbuf copy udplen */ 1042 if (offset + sizeof(struct udphdr) > m->m_len) { 1043 m_copydata(m, offset + offsetof(struct udphdr, 1044 uh_ulen), sizeof(cklen), (caddr_t)&cklen); 1045 cklen = ntohs(cklen); 1046 } else { 1047 uh = (struct udphdr *)mtodo(m, offset); 1048 cklen = ntohs(uh->uh_ulen); 1049 } 1050 csum = in_cksum_skip(m, cklen + offset, offset); 1051 if (csum == 0) 1052 csum = 0xffff; 1053 } else { 1054 cklen = ntohs(ip->ip_len); 1055 csum = in_cksum_skip(m, cklen, offset); 1056 } 1057 offset += m->m_pkthdr.csum_data; /* checksum offset */ 1058 1059 if (offset + sizeof(csum) > m->m_len) 1060 m_copyback(m, offset, sizeof(csum), (caddr_t)&csum); 1061 else 1062 *(u_short *)mtodo(m, offset) = csum; 1063 } 1064 1065 /* 1066 * IP socket option processing. 1067 */ 1068 int 1069 ip_ctloutput(struct socket *so, struct sockopt *sopt) 1070 { 1071 struct inpcb *inp = sotoinpcb(so); 1072 int error, optval; 1073 #ifdef RSS 1074 uint32_t rss_bucket; 1075 int retval; 1076 #endif 1077 1078 error = optval = 0; 1079 if (sopt->sopt_level != IPPROTO_IP) { 1080 error = EINVAL; 1081 1082 if (sopt->sopt_level == SOL_SOCKET && 1083 sopt->sopt_dir == SOPT_SET) { 1084 switch (sopt->sopt_name) { 1085 case SO_REUSEADDR: 1086 INP_WLOCK(inp); 1087 if ((so->so_options & SO_REUSEADDR) != 0) 1088 inp->inp_flags2 |= INP_REUSEADDR; 1089 else 1090 inp->inp_flags2 &= ~INP_REUSEADDR; 1091 INP_WUNLOCK(inp); 1092 error = 0; 1093 break; 1094 case SO_REUSEPORT: 1095 INP_WLOCK(inp); 1096 if ((so->so_options & SO_REUSEPORT) != 0) 1097 inp->inp_flags2 |= INP_REUSEPORT; 1098 else 1099 inp->inp_flags2 &= ~INP_REUSEPORT; 1100 INP_WUNLOCK(inp); 1101 error = 0; 1102 break; 1103 case SO_REUSEPORT_LB: 1104 INP_WLOCK(inp); 1105 if ((so->so_options & SO_REUSEPORT_LB) != 0) 1106 inp->inp_flags2 |= INP_REUSEPORT_LB; 1107 else 1108 inp->inp_flags2 &= ~INP_REUSEPORT_LB; 1109 INP_WUNLOCK(inp); 1110 error = 0; 1111 break; 1112 case SO_SETFIB: 1113 INP_WLOCK(inp); 1114 inp->inp_inc.inc_fibnum = so->so_fibnum; 1115 INP_WUNLOCK(inp); 1116 error = 0; 1117 break; 1118 case SO_MAX_PACING_RATE: 1119 #ifdef RATELIMIT 1120 INP_WLOCK(inp); 1121 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED; 1122 INP_WUNLOCK(inp); 1123 error = 0; 1124 #else 1125 error = EOPNOTSUPP; 1126 #endif 1127 break; 1128 default: 1129 break; 1130 } 1131 } 1132 return (error); 1133 } 1134 1135 switch (sopt->sopt_dir) { 1136 case SOPT_SET: 1137 switch (sopt->sopt_name) { 1138 case IP_OPTIONS: 1139 #ifdef notyet 1140 case IP_RETOPTS: 1141 #endif 1142 { 1143 struct mbuf *m; 1144 if (sopt->sopt_valsize > MLEN) { 1145 error = EMSGSIZE; 1146 break; 1147 } 1148 m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA); 1149 if (m == NULL) { 1150 error = ENOBUFS; 1151 break; 1152 } 1153 m->m_len = sopt->sopt_valsize; 1154 error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1155 m->m_len); 1156 if (error) { 1157 m_free(m); 1158 break; 1159 } 1160 INP_WLOCK(inp); 1161 error = ip_pcbopts(inp, sopt->sopt_name, m); 1162 INP_WUNLOCK(inp); 1163 return (error); 1164 } 1165 1166 case IP_BINDANY: 1167 if (sopt->sopt_td != NULL) { 1168 error = priv_check(sopt->sopt_td, 1169 PRIV_NETINET_BINDANY); 1170 if (error) 1171 break; 1172 } 1173 /* FALLTHROUGH */ 1174 case IP_BINDMULTI: 1175 #ifdef RSS 1176 case IP_RSS_LISTEN_BUCKET: 1177 #endif 1178 case IP_TOS: 1179 case IP_TTL: 1180 case IP_MINTTL: 1181 case IP_RECVOPTS: 1182 case IP_RECVRETOPTS: 1183 case IP_ORIGDSTADDR: 1184 case IP_RECVDSTADDR: 1185 case IP_RECVTTL: 1186 case IP_RECVIF: 1187 case IP_ONESBCAST: 1188 case IP_DONTFRAG: 1189 case IP_RECVTOS: 1190 case IP_RECVFLOWID: 1191 #ifdef RSS 1192 case IP_RECVRSSBUCKETID: 1193 #endif 1194 case IP_VLAN_PCP: 1195 error = sooptcopyin(sopt, &optval, sizeof optval, 1196 sizeof optval); 1197 if (error) 1198 break; 1199 1200 switch (sopt->sopt_name) { 1201 case IP_TOS: 1202 inp->inp_ip_tos = optval; 1203 break; 1204 1205 case IP_TTL: 1206 inp->inp_ip_ttl = optval; 1207 break; 1208 1209 case IP_MINTTL: 1210 if (optval >= 0 && optval <= MAXTTL) 1211 inp->inp_ip_minttl = optval; 1212 else 1213 error = EINVAL; 1214 break; 1215 1216 #define OPTSET(bit) do { \ 1217 INP_WLOCK(inp); \ 1218 if (optval) \ 1219 inp->inp_flags |= bit; \ 1220 else \ 1221 inp->inp_flags &= ~bit; \ 1222 INP_WUNLOCK(inp); \ 1223 } while (0) 1224 1225 #define OPTSET2(bit, val) do { \ 1226 INP_WLOCK(inp); \ 1227 if (val) \ 1228 inp->inp_flags2 |= bit; \ 1229 else \ 1230 inp->inp_flags2 &= ~bit; \ 1231 INP_WUNLOCK(inp); \ 1232 } while (0) 1233 1234 case IP_RECVOPTS: 1235 OPTSET(INP_RECVOPTS); 1236 break; 1237 1238 case IP_RECVRETOPTS: 1239 OPTSET(INP_RECVRETOPTS); 1240 break; 1241 1242 case IP_RECVDSTADDR: 1243 OPTSET(INP_RECVDSTADDR); 1244 break; 1245 1246 case IP_ORIGDSTADDR: 1247 OPTSET2(INP_ORIGDSTADDR, optval); 1248 break; 1249 1250 case IP_RECVTTL: 1251 OPTSET(INP_RECVTTL); 1252 break; 1253 1254 case IP_RECVIF: 1255 OPTSET(INP_RECVIF); 1256 break; 1257 1258 case IP_ONESBCAST: 1259 OPTSET(INP_ONESBCAST); 1260 break; 1261 case IP_DONTFRAG: 1262 OPTSET(INP_DONTFRAG); 1263 break; 1264 case IP_BINDANY: 1265 OPTSET(INP_BINDANY); 1266 break; 1267 case IP_RECVTOS: 1268 OPTSET(INP_RECVTOS); 1269 break; 1270 case IP_BINDMULTI: 1271 OPTSET2(INP_BINDMULTI, optval); 1272 break; 1273 case IP_RECVFLOWID: 1274 OPTSET2(INP_RECVFLOWID, optval); 1275 break; 1276 #ifdef RSS 1277 case IP_RSS_LISTEN_BUCKET: 1278 if ((optval >= 0) && 1279 (optval < rss_getnumbuckets())) { 1280 inp->inp_rss_listen_bucket = optval; 1281 OPTSET2(INP_RSS_BUCKET_SET, 1); 1282 } else { 1283 error = EINVAL; 1284 } 1285 break; 1286 case IP_RECVRSSBUCKETID: 1287 OPTSET2(INP_RECVRSSBUCKETID, optval); 1288 break; 1289 #endif 1290 case IP_VLAN_PCP: 1291 if ((optval >= -1) && (optval <= 1292 (INP_2PCP_MASK >> INP_2PCP_SHIFT))) { 1293 if (optval == -1) { 1294 INP_WLOCK(inp); 1295 inp->inp_flags2 &= 1296 ~(INP_2PCP_SET | 1297 INP_2PCP_MASK); 1298 INP_WUNLOCK(inp); 1299 } else { 1300 INP_WLOCK(inp); 1301 inp->inp_flags2 |= 1302 INP_2PCP_SET; 1303 inp->inp_flags2 &= 1304 ~INP_2PCP_MASK; 1305 inp->inp_flags2 |= 1306 optval << INP_2PCP_SHIFT; 1307 INP_WUNLOCK(inp); 1308 } 1309 } else 1310 error = EINVAL; 1311 break; 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 case IP_VLAN_PCP: 1433 switch (sopt->sopt_name) { 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 case IP_VLAN_PCP: 1521 if (OPTBIT2(INP_2PCP_SET)) { 1522 optval = (inp->inp_flags2 & 1523 INP_2PCP_MASK) >> INP_2PCP_SHIFT; 1524 } else { 1525 optval = -1; 1526 } 1527 break; 1528 } 1529 error = sooptcopyout(sopt, &optval, sizeof optval); 1530 break; 1531 1532 /* 1533 * Multicast socket options are processed by the in_mcast 1534 * module. 1535 */ 1536 case IP_MULTICAST_IF: 1537 case IP_MULTICAST_VIF: 1538 case IP_MULTICAST_TTL: 1539 case IP_MULTICAST_LOOP: 1540 case IP_MSFILTER: 1541 error = inp_getmoptions(inp, sopt); 1542 break; 1543 1544 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 1545 case IP_IPSEC_POLICY: 1546 if (IPSEC_ENABLED(ipv4)) { 1547 error = IPSEC_PCBCTL(ipv4, inp, sopt); 1548 break; 1549 } 1550 /* FALLTHROUGH */ 1551 #endif /* IPSEC */ 1552 1553 default: 1554 error = ENOPROTOOPT; 1555 break; 1556 } 1557 break; 1558 } 1559 return (error); 1560 } 1561 1562 /* 1563 * Routine called from ip_output() to loop back a copy of an IP multicast 1564 * packet to the input queue of a specified interface. Note that this 1565 * calls the output routine of the loopback "driver", but with an interface 1566 * pointer that might NOT be a loopback interface -- evil, but easier than 1567 * replicating that code here. 1568 */ 1569 static void 1570 ip_mloopback(struct ifnet *ifp, const struct mbuf *m, int hlen) 1571 { 1572 struct ip *ip; 1573 struct mbuf *copym; 1574 1575 /* 1576 * Make a deep copy of the packet because we're going to 1577 * modify the pack in order to generate checksums. 1578 */ 1579 copym = m_dup(m, M_NOWAIT); 1580 if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen)) 1581 copym = m_pullup(copym, hlen); 1582 if (copym != NULL) { 1583 /* If needed, compute the checksum and mark it as valid. */ 1584 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1585 in_delayed_cksum(copym); 1586 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1587 copym->m_pkthdr.csum_flags |= 1588 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1589 copym->m_pkthdr.csum_data = 0xffff; 1590 } 1591 /* 1592 * We don't bother to fragment if the IP length is greater 1593 * than the interface's MTU. Can this possibly matter? 1594 */ 1595 ip = mtod(copym, struct ip *); 1596 ip->ip_sum = 0; 1597 ip->ip_sum = in_cksum(copym, hlen); 1598 if_simloop(ifp, copym, AF_INET, 0); 1599 } 1600 } 1601