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