1 /* $NetBSD: ip_output.c,v 1.330 2025/07/17 06:48:39 ozaki-r Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * 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 project 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 PROJECT 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 PROJECT 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 32 /* 33 * Copyright (c) 1998 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Public Access Networks Corporation ("Panix"). It was developed under 38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 59 * POSSIBILITY OF SUCH DAMAGE. 60 */ 61 62 /* 63 * Copyright (c) 1982, 1986, 1988, 1990, 1993 64 * The Regents of the University of California. All rights reserved. 65 * 66 * Redistribution and use in source and binary forms, with or without 67 * modification, are permitted provided that the following conditions 68 * are met: 69 * 1. Redistributions of source code must retain the above copyright 70 * notice, this list of conditions and the following disclaimer. 71 * 2. Redistributions in binary form must reproduce the above copyright 72 * notice, this list of conditions and the following disclaimer in the 73 * documentation and/or other materials provided with the distribution. 74 * 3. Neither the name of the University nor the names of its contributors 75 * may be used to endorse or promote products derived from this software 76 * without specific prior written permission. 77 * 78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 88 * SUCH DAMAGE. 89 * 90 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 91 */ 92 93 #include <sys/cdefs.h> 94 __KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.330 2025/07/17 06:48:39 ozaki-r Exp $"); 95 96 #ifdef _KERNEL_OPT 97 #include "opt_inet.h" 98 #include "opt_ipsec.h" 99 #include "opt_mrouting.h" 100 #include "opt_net_mpsafe.h" 101 #include "opt_mpls.h" 102 #endif 103 104 #include "arp.h" 105 106 #include <sys/param.h> 107 #include <sys/kmem.h> 108 #include <sys/mbuf.h> 109 #include <sys/socket.h> 110 #include <sys/socketvar.h> 111 #include <sys/kauth.h> 112 #include <sys/systm.h> 113 #include <sys/syslog.h> 114 115 #include <net/if.h> 116 #include <net/if_types.h> 117 #include <net/route.h> 118 #include <net/pfil.h> 119 120 #include <netinet/in.h> 121 #include <netinet/in_systm.h> 122 #include <netinet/ip.h> 123 #include <netinet/in_pcb.h> 124 #include <netinet/in_var.h> 125 #include <netinet/ip_var.h> 126 #include <netinet/ip_private.h> 127 #include <netinet/in_offload.h> 128 #include <netinet/portalgo.h> 129 #include <netinet/udp.h> 130 #include <netinet/udp_var.h> 131 132 #ifdef INET6 133 #include <netinet6/ip6_var.h> 134 #endif 135 136 #ifdef MROUTING 137 #include <netinet/ip_mroute.h> 138 #endif 139 140 #ifdef IPSEC 141 #include <netipsec/ipsec.h> 142 #include <netipsec/key.h> 143 #endif 144 145 #ifdef MPLS 146 #include <netmpls/mpls.h> 147 #include <netmpls/mpls_var.h> 148 #endif 149 150 static int ip_pcbopts(struct inpcb *, const struct sockopt *); 151 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *); 152 static struct ifnet *ip_multicast_if(struct in_addr *, int *); 153 static void ip_mloopback(struct ifnet *, struct mbuf *, 154 const struct sockaddr_in *); 155 static int ip_ifaddrvalid(const struct in_ifaddr *); 156 157 extern pfil_head_t *inet_pfil_hook; /* XXX */ 158 159 int ip_do_loopback_cksum = 0; 160 161 static int 162 ip_mark_mpls(struct ifnet * const ifp, struct mbuf * const m, 163 const struct rtentry *rt) 164 { 165 int error = 0; 166 #ifdef MPLS 167 union mpls_shim msh; 168 169 if (rt == NULL || rt_gettag(rt) == NULL || 170 rt_gettag(rt)->sa_family != AF_MPLS || 171 (m->m_flags & (M_MCAST | M_BCAST)) != 0 || 172 ifp->if_type != IFT_ETHER) 173 return 0; 174 175 msh.s_addr = MPLS_GETSADDR(rt); 176 if (msh.shim.label != MPLS_LABEL_IMPLNULL) { 177 struct m_tag *mtag; 178 /* 179 * XXX tentative solution to tell ether_output 180 * it's MPLS. Need some more efficient solution. 181 */ 182 mtag = m_tag_get(PACKET_TAG_MPLS, 183 sizeof(int) /* dummy */, 184 M_NOWAIT); 185 if (mtag == NULL) 186 return ENOMEM; 187 m_tag_prepend(m, mtag); 188 } 189 #endif 190 return error; 191 } 192 193 /* 194 * Send an IP packet to a host. 195 */ 196 int 197 ip_if_output(struct ifnet * const ifp, struct mbuf * const m, 198 const struct sockaddr * const dst, const struct rtentry *rt) 199 { 200 int error = 0; 201 202 if (rt != NULL) { 203 error = rt_check_reject_route(rt, ifp); 204 if (error != 0) { 205 IP_STATINC(IP_STAT_RTREJECT); 206 m_freem(m); 207 return error; 208 } 209 } 210 211 error = ip_mark_mpls(ifp, m, rt); 212 if (error != 0) { 213 m_freem(m); 214 return error; 215 } 216 217 error = if_output_lock(ifp, ifp, m, dst, rt); 218 219 return error; 220 } 221 222 /* 223 * IP output. The packet in mbuf chain m contains a skeletal IP 224 * header (with len, off, ttl, proto, tos, src, dst). 225 * The mbuf chain containing the packet will be freed. 226 * The mbuf opt, if present, will not be freed. 227 */ 228 int 229 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, int flags, 230 struct ip_moptions *imo, struct inpcb *inp) 231 { 232 struct rtentry *rt; 233 struct ip *ip; 234 struct ifnet *ifp, *mifp = NULL; 235 struct mbuf *m = m0; 236 int len, hlen, error = 0; 237 struct route iproute; 238 const struct sockaddr_in *dst; 239 struct in_ifaddr *ia = NULL; 240 int isbroadcast; 241 int sw_csum; 242 u_long mtu; 243 bool natt_frag = false; 244 bool rtmtu_nolock; 245 union { 246 struct sockaddr sa; 247 struct sockaddr_in sin; 248 } udst, usrc; 249 struct sockaddr *rdst = &udst.sa; /* real IP destination, as 250 * opposed to the nexthop 251 */ 252 struct psref psref, psref_ia; 253 int bound; 254 bool bind_need_restore = false; 255 const struct sockaddr *sa; 256 bool need_ia4_release = false; 257 258 len = 0; 259 260 MCLAIM(m, &ip_tx_mowner); 261 262 KASSERT((m->m_flags & M_PKTHDR) != 0); 263 KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) == 0); 264 KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) != 265 (M_CSUM_TCPv4|M_CSUM_UDPv4)); 266 KASSERT(m->m_len >= sizeof(struct ip)); 267 268 hlen = sizeof(struct ip); 269 if (opt) { 270 m = ip_insertoptions(m, opt, &len); 271 hlen = len; 272 } 273 ip = mtod(m, struct ip *); 274 275 /* 276 * Fill in IP header. 277 */ 278 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 279 ip->ip_v = IPVERSION; 280 ip->ip_off = htons(0); 281 /* ip->ip_id filled in after we find out source ia */ 282 ip->ip_hl = hlen >> 2; 283 IP_STATINC(IP_STAT_LOCALOUT); 284 } else { 285 hlen = ip->ip_hl << 2; 286 } 287 288 /* 289 * Route packet. 290 */ 291 if (ro == NULL) { 292 memset(&iproute, 0, sizeof(iproute)); 293 ro = &iproute; 294 } 295 sockaddr_in_init(&udst.sin, &ip->ip_dst, 0); 296 dst = satocsin(rtcache_getdst(ro)); 297 298 /* 299 * If there is a cached route, check that it is to the same 300 * destination and is still up. If not, free it and try again. 301 * The address family should also be checked in case of sharing 302 * the cache with IPv6. 303 */ 304 if (dst && (dst->sin_family != AF_INET || 305 !in_hosteq(dst->sin_addr, ip->ip_dst))) 306 rtcache_free(ro); 307 308 /* XXX must be before rtcache operations */ 309 bound = curlwp_bind(); 310 bind_need_restore = true; 311 312 if ((rt = rtcache_validate(ro)) == NULL && 313 (rt = rtcache_update(ro, 1)) == NULL) { 314 dst = &udst.sin; 315 error = rtcache_setdst(ro, &udst.sa); 316 if (error != 0) { 317 IP_STATINC(IP_STAT_ODROPPED); 318 goto bad; 319 } 320 } 321 322 /* 323 * If routing to interface only, short circuit routing lookup. 324 */ 325 if (flags & IP_ROUTETOIF) { 326 struct ifaddr *ifa = ifa_ifwithladdr_psref(sintocsa(dst), &psref_ia); 327 if (ifa == NULL) { 328 IP_STATINC(IP_STAT_NOROUTE); 329 error = ENETUNREACH; 330 goto bad; 331 } 332 /* ia is already referenced by psref_ia */ 333 ia = ifatoia(ifa); 334 need_ia4_release = true; 335 336 /* Need a reference to keep ifp after ia4_release(ia). */ 337 ifp = mifp = if_get_byindex(ia->ia_ifp->if_index, &psref); 338 if (__predict_false(ifp == NULL)) { 339 IP_STATINC(IP_STAT_NOROUTE); 340 error = ENETUNREACH; 341 goto bad; 342 } 343 mtu = ifp->if_mtu; 344 ip->ip_ttl = 1; 345 isbroadcast = in_broadcast(dst->sin_addr, ifp); 346 } else if (((IN_MULTICAST(ip->ip_dst.s_addr) || 347 ip->ip_dst.s_addr == INADDR_BROADCAST) || 348 (flags & IP_ROUTETOIFINDEX)) && 349 imo != NULL && imo->imo_multicast_if_index != 0) { 350 ifp = mifp = if_get_byindex(imo->imo_multicast_if_index, &psref); 351 if (ifp == NULL) { 352 IP_STATINC(IP_STAT_NOROUTE); 353 error = ENETUNREACH; 354 goto bad; 355 } 356 mtu = ifp->if_mtu; 357 ia = in_get_ia_from_ifp_psref(ifp, &psref_ia); 358 need_ia4_release = true; 359 if (IN_MULTICAST(ip->ip_dst.s_addr) || 360 ip->ip_dst.s_addr == INADDR_BROADCAST) { 361 isbroadcast = 0; 362 } else { 363 /* IP_ROUTETOIFINDEX */ 364 isbroadcast = in_broadcast(dst->sin_addr, ifp); 365 if ((isbroadcast == 0) && ((ifp->if_flags & 366 (IFF_LOOPBACK | IFF_POINTOPOINT)) == 0) && 367 (in_direct(dst->sin_addr, ifp) == 0)) { 368 /* gateway address required */ 369 if (rt == NULL) 370 rt = rtcache_init(ro); 371 if (rt == NULL || rt->rt_ifp != ifp) { 372 IP_STATINC(IP_STAT_NOROUTE); 373 error = EHOSTUNREACH; 374 goto bad; 375 } 376 rt->rt_use++; 377 if (rt->rt_flags & RTF_GATEWAY) 378 dst = satosin(rt->rt_gateway); 379 if (rt->rt_flags & RTF_HOST) 380 isbroadcast = 381 rt->rt_flags & RTF_BROADCAST; 382 } 383 } 384 } else { 385 if (rt == NULL) 386 rt = rtcache_init(ro); 387 if (rt == NULL) { 388 IP_STATINC(IP_STAT_NOROUTE); 389 error = EHOSTUNREACH; 390 goto bad; 391 } 392 /* 393 * Taking a psref of ifa via rt_ifa is racy, so use it as is, which 394 * is safe because rt_ifa is not freed during rt is held. 395 */ 396 ia = ifatoia(rt->rt_ifa); 397 ifp = rt->rt_ifp; 398 if ((mtu = rt->rt_rmx.rmx_mtu) == 0) 399 mtu = ifp->if_mtu; 400 rt->rt_use++; 401 if (rt->rt_flags & RTF_GATEWAY) 402 dst = satosin(rt->rt_gateway); 403 if (rt->rt_flags & RTF_HOST) 404 isbroadcast = rt->rt_flags & RTF_BROADCAST; 405 else 406 isbroadcast = in_broadcast(dst->sin_addr, ifp); 407 } 408 rtmtu_nolock = rt && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0; 409 410 if (IN_MULTICAST(ip->ip_dst.s_addr) || 411 (ip->ip_dst.s_addr == INADDR_BROADCAST)) { 412 bool inmgroup; 413 414 m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ? 415 M_BCAST : M_MCAST; 416 /* 417 * See if the caller provided any multicast options 418 */ 419 if (imo != NULL) 420 ip->ip_ttl = imo->imo_multicast_ttl; 421 else 422 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 423 424 /* 425 * if we don't know the outgoing ifp yet, we can't generate 426 * output 427 */ 428 if (!ifp) { 429 IP_STATINC(IP_STAT_NOROUTE); 430 error = ENETUNREACH; 431 goto bad; 432 } 433 434 /* 435 * If the packet is multicast or broadcast, confirm that 436 * the outgoing interface can transmit it. 437 */ 438 if (((m->m_flags & M_MCAST) && 439 (ifp->if_flags & IFF_MULTICAST) == 0) || 440 ((m->m_flags & M_BCAST) && 441 (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)) { 442 IP_STATINC(IP_STAT_NOROUTE); 443 error = ENETUNREACH; 444 goto bad; 445 } 446 /* 447 * If source address not specified yet, use an address 448 * of outgoing interface. 449 */ 450 if (in_nullhost(ip->ip_src)) { 451 struct in_ifaddr *xia; 452 struct ifaddr *ifa; 453 struct psref _psref; 454 455 xia = in_get_ia_from_ifp_psref(ifp, &_psref); 456 if (!xia) { 457 IP_STATINC(IP_STAT_IFNOADDR); 458 error = EADDRNOTAVAIL; 459 goto bad; 460 } 461 ifa = &xia->ia_ifa; 462 if (ifa->ifa_getifa != NULL) { 463 ia4_release(xia, &_psref); 464 /* FIXME ifa_getifa is NOMPSAFE */ 465 xia = ifatoia((*ifa->ifa_getifa)(ifa, rdst)); 466 if (xia == NULL) { 467 IP_STATINC(IP_STAT_IFNOADDR); 468 error = EADDRNOTAVAIL; 469 goto bad; 470 } 471 ia4_acquire(xia, &_psref); 472 } 473 ip->ip_src = xia->ia_addr.sin_addr; 474 ia4_release(xia, &_psref); 475 } 476 477 inmgroup = in_multi_group(ip->ip_dst, ifp, flags); 478 if (inmgroup && (imo == NULL || imo->imo_multicast_loop)) { 479 /* 480 * If we belong to the destination multicast group 481 * on the outgoing interface, and the caller did not 482 * forbid loopback, loop back a copy. 483 */ 484 ip_mloopback(ifp, m, &udst.sin); 485 } 486 #ifdef MROUTING 487 else { 488 /* 489 * If we are acting as a multicast router, perform 490 * multicast forwarding as if the packet had just 491 * arrived on the interface to which we are about 492 * to send. The multicast forwarding function 493 * recursively calls this function, using the 494 * IP_FORWARDING flag to prevent infinite recursion. 495 * 496 * Multicasts that are looped back by ip_mloopback(), 497 * above, will be forwarded by the ip_input() routine, 498 * if necessary. 499 */ 500 extern struct socket *ip_mrouter; 501 502 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 503 if (ip_mforward(m, ifp) != 0) { 504 m_freem(m); 505 goto done; 506 } 507 } 508 } 509 #endif 510 /* 511 * Multicasts with a time-to-live of zero may be looped- 512 * back, above, but must not be transmitted on a network. 513 * Also, multicasts addressed to the loopback interface 514 * are not sent -- the above call to ip_mloopback() will 515 * loop back a copy if this host actually belongs to the 516 * destination group on the loopback interface. 517 */ 518 if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) { 519 IP_STATINC(IP_STAT_ODROPPED); 520 m_freem(m); 521 goto done; 522 } 523 goto sendit; 524 } 525 526 /* 527 * If source address not specified yet, use address 528 * of outgoing interface. 529 */ 530 if (in_nullhost(ip->ip_src)) { 531 struct ifaddr *ifa; 532 533 ifa = &ia->ia_ifa; 534 if (ifa->ifa_getifa != NULL) { 535 if (need_ia4_release) { 536 ia4_release(ia, &psref_ia); 537 need_ia4_release = false; 538 } 539 /* FIXME ifa_getifa is NOMPSAFE */ 540 ia = ifatoia((*ifa->ifa_getifa)(ifa, rdst)); 541 if (ia == NULL) { 542 error = EADDRNOTAVAIL; 543 goto bad; 544 } 545 ia4_acquire(ia, &psref_ia); 546 need_ia4_release = true; 547 } 548 ip->ip_src = ia->ia_addr.sin_addr; 549 } 550 551 /* 552 * Packets with Class-D address as source are not valid per 553 * RFC1112. 554 */ 555 if (IN_MULTICAST(ip->ip_src.s_addr)) { 556 IP_STATINC(IP_STAT_ODROPPED); 557 error = EADDRNOTAVAIL; 558 goto bad; 559 } 560 561 /* 562 * Look for broadcast address and verify user is allowed to 563 * send such a packet. 564 */ 565 if (isbroadcast) { 566 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 567 IP_STATINC(IP_STAT_BCASTDENIED); 568 error = EADDRNOTAVAIL; 569 goto bad; 570 } 571 if ((flags & IP_ALLOWBROADCAST) == 0) { 572 IP_STATINC(IP_STAT_BCASTDENIED); 573 error = EACCES; 574 goto bad; 575 } 576 /* don't allow broadcast messages to be fragmented */ 577 if (ntohs(ip->ip_len) > ifp->if_mtu) { 578 IP_STATINC(IP_STAT_BCASTDENIED); 579 error = EMSGSIZE; 580 goto bad; 581 } 582 m->m_flags |= M_BCAST; 583 } else 584 m->m_flags &= ~M_BCAST; 585 586 sendit: 587 if ((flags & (IP_FORWARDING|IP_NOIPNEWID)) == 0) { 588 if (m->m_pkthdr.len < IP_MINFRAGSIZE) { 589 ip->ip_id = 0; 590 } else if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) { 591 ip->ip_id = ip_newid(); 592 } else { 593 /* 594 * TSO capable interfaces (typically?) increment 595 * ip_id for each segment. 596 * "allocate" enough ids here to increase the chance 597 * for them to be unique. 598 * 599 * note that the following calculation is not 600 * needed to be precise. wasting some ip_id is fine. 601 */ 602 603 unsigned int segsz = m->m_pkthdr.segsz; 604 unsigned int datasz = ntohs(ip->ip_len) - hlen; 605 unsigned int num = howmany(datasz, segsz); 606 607 ip->ip_id = ip_newid_range(num); 608 } 609 } 610 if (ia != NULL) { 611 if (need_ia4_release) { 612 ia4_release(ia, &psref_ia); 613 need_ia4_release = false; 614 } 615 ia = NULL; 616 } 617 618 /* 619 * If we're doing Path MTU Discovery, we need to set DF unless 620 * the route's MTU is locked. 621 */ 622 if ((flags & IP_MTUDISC) != 0 && rtmtu_nolock) { 623 ip->ip_off |= htons(IP_DF); 624 } 625 626 #ifdef IPSEC 627 if (ipsec_used) { 628 bool ipsec_done = false; 629 bool count_drop = false; 630 631 /* Perform IPsec processing, if any. */ 632 error = ipsec4_output(m, inp, flags, &mtu, &natt_frag, 633 &ipsec_done, &count_drop); 634 if (count_drop) 635 IP_STATINC(IP_STAT_IPSECDROP_OUT); 636 if (error || ipsec_done) 637 goto done; 638 } 639 640 if (!ipsec_used || !natt_frag) 641 #endif 642 { 643 /* 644 * Run through list of hooks for output packets. 645 */ 646 error = pfil_run_hooks(inet_pfil_hook, &m, ifp, PFIL_OUT); 647 if (error || m == NULL) { 648 IP_STATINC(IP_STAT_PFILDROP_OUT); 649 goto done; 650 } 651 } 652 653 ip = mtod(m, struct ip *); 654 hlen = ip->ip_hl << 2; 655 656 m->m_pkthdr.csum_data |= hlen << 16; 657 658 /* 659 * search for the source address structure to 660 * maintain output statistics, and verify address 661 * validity 662 */ 663 KASSERT(ia == NULL); 664 sockaddr_in_init(&usrc.sin, &ip->ip_src, 0); 665 ia = ifatoia(ifaof_ifpforaddr_psref(&usrc.sa, ifp, &psref_ia)); 666 if (ia != NULL) 667 need_ia4_release = true; 668 669 /* 670 * Ensure we only send from a valid address. 671 * A NULL address is valid because the packet could be 672 * generated from a packet filter. 673 */ 674 if (ia != NULL && (flags & IP_FORWARDING) == 0 && 675 (error = ip_ifaddrvalid(ia)) != 0) 676 { 677 ARPLOG(LOG_ERR, 678 "refusing to send from invalid address %s (pid %d)\n", 679 ARPLOGADDR(&ip->ip_src), curproc->p_pid); 680 IP_STATINC(IP_STAT_ODROPPED); 681 if (error == 1) 682 /* 683 * Address exists, but is tentative or detached. 684 * We can't send from it because it's invalid, 685 * so we drop the packet. 686 */ 687 error = 0; 688 else 689 error = EADDRNOTAVAIL; 690 goto bad; 691 } 692 693 /* Maybe skip checksums on loopback interfaces. */ 694 if (IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) { 695 m->m_pkthdr.csum_flags |= M_CSUM_IPv4; 696 } 697 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx; 698 699 /* Need to fragment the packet */ 700 if (ntohs(ip->ip_len) > mtu && 701 (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) { 702 goto fragment; 703 } 704 705 #if IFA_STATS 706 if (ia) 707 ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len); 708 #endif 709 /* 710 * Always initialize the sum to 0! Some HW assisted 711 * checksumming requires this. 712 */ 713 ip->ip_sum = 0; 714 715 if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) { 716 /* 717 * Perform any checksums that the hardware can't do 718 * for us. 719 * 720 * XXX Does any hardware require the {th,uh}_sum 721 * XXX fields to be 0? 722 */ 723 if (sw_csum & M_CSUM_IPv4) { 724 KASSERT(IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)); 725 ip->ip_sum = in_cksum(m, hlen); 726 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4; 727 } 728 if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 729 if (IN_NEED_CHECKSUM(ifp, 730 sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4))) { 731 in_undefer_cksum_tcpudp(m); 732 } 733 m->m_pkthdr.csum_flags &= 734 ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 735 } 736 } 737 738 sa = (m->m_flags & M_MCAST) ? sintocsa(rdst) : sintocsa(dst); 739 740 /* Send it */ 741 if (__predict_false(sw_csum & M_CSUM_TSOv4)) { 742 /* 743 * TSO4 is required by a packet, but disabled for 744 * the interface. 745 */ 746 error = ip_tso_output(ifp, m, sa, rt); 747 } else 748 error = ip_if_output(ifp, m, sa, rt); 749 goto done; 750 751 fragment: 752 /* 753 * We can't use HW checksumming if we're about to fragment the packet. 754 * 755 * XXX Some hardware can do this. 756 */ 757 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 758 if (IN_NEED_CHECKSUM(ifp, 759 m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))) { 760 in_undefer_cksum_tcpudp(m); 761 } 762 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 763 } 764 765 /* 766 * Too large for interface; fragment if possible. 767 * Must be able to put at least 8 bytes per fragment. 768 */ 769 if (ntohs(ip->ip_off) & IP_DF) { 770 if (flags & IP_RETURNMTU) { 771 KASSERT(inp != NULL); 772 in4p_errormtu(inp) = mtu; 773 } 774 error = EMSGSIZE; 775 IP_STATINC(IP_STAT_CANTFRAG); 776 goto bad; 777 } 778 779 error = ip_fragment(m, ifp, mtu); 780 if (error) { 781 m = NULL; 782 goto bad; 783 } 784 785 for (; m; m = m0) { 786 m0 = m->m_nextpkt; 787 m->m_nextpkt = NULL; 788 if (error) { 789 m_freem(m); 790 continue; 791 } 792 #if IFA_STATS 793 if (ia) 794 ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len); 795 #endif 796 /* 797 * If we get there, the packet has not been handled by 798 * IPsec whereas it should have. Now that it has been 799 * fragmented, re-inject it in ip_output so that IPsec 800 * processing can occur. 801 */ 802 if (natt_frag) { 803 error = ip_output(m, opt, NULL, 804 flags | IP_RAWOUTPUT | IP_NOIPNEWID, 805 imo, inp); 806 } else { 807 KASSERT((m->m_pkthdr.csum_flags & 808 (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0); 809 error = ip_if_output(ifp, m, (m->m_flags & M_MCAST) ? 810 sintocsa(rdst) : sintocsa(dst), rt); 811 } 812 } 813 if (error == 0) { 814 IP_STATINC(IP_STAT_FRAGMENTED); 815 } 816 817 done: 818 if (need_ia4_release) 819 ia4_release(ia, &psref_ia); 820 rtcache_unref(rt, ro); 821 if (ro == &iproute) { 822 rtcache_free(&iproute); 823 } 824 if (mifp != NULL) { 825 if_put(mifp, &psref); 826 } 827 if (bind_need_restore) 828 curlwp_bindx(bound); 829 return error; 830 831 bad: 832 m_freem(m); 833 goto done; 834 } 835 836 int 837 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu) 838 { 839 struct ip *ip, *mhip; 840 struct mbuf *m0; 841 int len, hlen, off; 842 int mhlen, firstlen; 843 struct mbuf **mnext; 844 int sw_csum = m->m_pkthdr.csum_flags; 845 int fragments = 0; 846 int error = 0; 847 int ipoff, ipflg; 848 849 ip = mtod(m, struct ip *); 850 hlen = ip->ip_hl << 2; 851 852 /* Preserve the offset and flags. */ 853 ipoff = ntohs(ip->ip_off) & IP_OFFMASK; 854 ipflg = ntohs(ip->ip_off) & (IP_RF|IP_DF|IP_MF); 855 856 if (ifp != NULL) 857 sw_csum &= ~ifp->if_csum_flags_tx; 858 859 len = (mtu - hlen) &~ 7; 860 if (len < 8) { 861 IP_STATINC(IP_STAT_CANTFRAG); 862 m_freem(m); 863 return EMSGSIZE; 864 } 865 866 firstlen = len; 867 mnext = &m->m_nextpkt; 868 869 /* 870 * Loop through length of segment after first fragment, 871 * make new header and copy data of each part and link onto chain. 872 */ 873 m0 = m; 874 mhlen = sizeof(struct ip); 875 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) { 876 MGETHDR(m, M_DONTWAIT, MT_HEADER); 877 if (m == NULL) { 878 error = ENOBUFS; 879 IP_STATINC(IP_STAT_ODROPPED); 880 goto sendorfree; 881 } 882 MCLAIM(m, m0->m_owner); 883 884 *mnext = m; 885 mnext = &m->m_nextpkt; 886 887 m->m_data += max_linkhdr; 888 mhip = mtod(m, struct ip *); 889 *mhip = *ip; 890 891 /* we must inherit the flags */ 892 m->m_flags |= m0->m_flags & M_COPYFLAGS; 893 894 if (hlen > sizeof(struct ip)) { 895 mhlen = ip_optcopy(ip, mhip) + sizeof(struct ip); 896 mhip->ip_hl = mhlen >> 2; 897 } 898 m->m_len = mhlen; 899 900 mhip->ip_off = ((off - hlen) >> 3) + ipoff; 901 mhip->ip_off |= ipflg; 902 if (off + len >= ntohs(ip->ip_len)) 903 len = ntohs(ip->ip_len) - off; 904 else 905 mhip->ip_off |= IP_MF; 906 HTONS(mhip->ip_off); 907 908 mhip->ip_len = htons((u_int16_t)(len + mhlen)); 909 m->m_next = m_copym(m0, off, len, M_DONTWAIT); 910 if (m->m_next == NULL) { 911 error = ENOBUFS; 912 IP_STATINC(IP_STAT_ODROPPED); 913 goto sendorfree; 914 } 915 916 m->m_pkthdr.len = mhlen + len; 917 m_reset_rcvif(m); 918 919 mhip->ip_sum = 0; 920 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0); 921 if (sw_csum & M_CSUM_IPv4) { 922 mhip->ip_sum = in_cksum(m, mhlen); 923 } else { 924 /* 925 * checksum is hw-offloaded or not necessary. 926 */ 927 m->m_pkthdr.csum_flags |= 928 m0->m_pkthdr.csum_flags & M_CSUM_IPv4; 929 m->m_pkthdr.csum_data |= mhlen << 16; 930 KASSERT(!(ifp != NULL && 931 IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) || 932 (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0); 933 } 934 IP_STATINC(IP_STAT_OFRAGMENTS); 935 fragments++; 936 } 937 938 /* 939 * Update first fragment by trimming what's been copied out 940 * and updating header, then send each fragment (in order). 941 */ 942 m = m0; 943 m_adj(m, hlen + firstlen - ntohs(ip->ip_len)); 944 m->m_pkthdr.len = hlen + firstlen; 945 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len); 946 ip->ip_off |= htons(IP_MF); 947 ip->ip_sum = 0; 948 if (sw_csum & M_CSUM_IPv4) { 949 ip->ip_sum = in_cksum(m, hlen); 950 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4; 951 } else { 952 /* 953 * checksum is hw-offloaded or not necessary. 954 */ 955 KASSERT(!(ifp != NULL && IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) || 956 (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0); 957 KASSERT(M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data) >= 958 sizeof(struct ip)); 959 } 960 961 sendorfree: 962 /* 963 * If there is no room for all the fragments, don't queue 964 * any of them. 965 */ 966 if (ifp != NULL) { 967 IFQ_LOCK(&ifp->if_snd); 968 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments && 969 error == 0) { 970 error = ENOBUFS; 971 IP_STATINC(IP_STAT_ODROPPED); 972 IFQ_INC_DROPS(&ifp->if_snd); 973 } 974 IFQ_UNLOCK(&ifp->if_snd); 975 } 976 if (error) { 977 for (m = m0; m; m = m0) { 978 m0 = m->m_nextpkt; 979 m->m_nextpkt = NULL; 980 m_freem(m); 981 } 982 } 983 984 return error; 985 } 986 987 /* 988 * Determine the maximum length of the options to be inserted; 989 * we would far rather allocate too much space rather than too little. 990 */ 991 u_int 992 ip_optlen(struct inpcb *inp) 993 { 994 struct mbuf *m = inp->inp_options; 995 996 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst)) { 997 return (m->m_len - offsetof(struct ipoption, ipopt_dst)); 998 } 999 return 0; 1000 } 1001 1002 /* 1003 * Insert IP options into preformed packet. 1004 * Adjust IP destination as required for IP source routing, 1005 * as indicated by a non-zero in_addr at the start of the options. 1006 */ 1007 static struct mbuf * 1008 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen) 1009 { 1010 struct ipoption *p = mtod(opt, struct ipoption *); 1011 struct mbuf *n; 1012 struct ip *ip = mtod(m, struct ip *); 1013 unsigned optlen; 1014 1015 optlen = opt->m_len - sizeof(p->ipopt_dst); 1016 KASSERT(optlen % 4 == 0); 1017 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) 1018 return m; /* XXX should fail */ 1019 if (!in_nullhost(p->ipopt_dst)) 1020 ip->ip_dst = p->ipopt_dst; 1021 if (M_READONLY(m) || M_LEADINGSPACE(m) < optlen) { 1022 MGETHDR(n, M_DONTWAIT, MT_HEADER); 1023 if (n == NULL) 1024 return m; 1025 MCLAIM(n, m->m_owner); 1026 m_move_pkthdr(n, m); 1027 m->m_len -= sizeof(struct ip); 1028 m->m_data += sizeof(struct ip); 1029 n->m_next = m; 1030 n->m_len = optlen + sizeof(struct ip); 1031 n->m_data += max_linkhdr; 1032 memcpy(mtod(n, void *), ip, sizeof(struct ip)); 1033 m = n; 1034 } else { 1035 m->m_data -= optlen; 1036 m->m_len += optlen; 1037 memmove(mtod(m, void *), ip, sizeof(struct ip)); 1038 } 1039 m->m_pkthdr.len += optlen; 1040 ip = mtod(m, struct ip *); 1041 memcpy(ip + 1, p->ipopt_list, optlen); 1042 *phlen = sizeof(struct ip) + optlen; 1043 ip->ip_len = htons(ntohs(ip->ip_len) + optlen); 1044 return m; 1045 } 1046 1047 /* 1048 * Copy options from ipsrc to ipdst, omitting those not copied during 1049 * fragmentation. 1050 */ 1051 int 1052 ip_optcopy(struct ip *ipsrc, struct ip *ipdst) 1053 { 1054 u_char *cp, *dp; 1055 int opt, optlen, cnt; 1056 1057 cp = (u_char *)(ipsrc + 1); 1058 dp = (u_char *)(ipdst + 1); 1059 cnt = (ipsrc->ip_hl << 2) - sizeof(struct ip); 1060 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1061 opt = cp[0]; 1062 if (opt == IPOPT_EOL) 1063 break; 1064 if (opt == IPOPT_NOP) { 1065 /* Preserve for IP mcast tunnel's LSRR alignment. */ 1066 *dp++ = IPOPT_NOP; 1067 optlen = 1; 1068 continue; 1069 } 1070 1071 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp)); 1072 optlen = cp[IPOPT_OLEN]; 1073 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen < cnt); 1074 1075 /* Invalid lengths should have been caught by ip_dooptions. */ 1076 if (optlen > cnt) 1077 optlen = cnt; 1078 if (IPOPT_COPIED(opt)) { 1079 bcopy((void *)cp, (void *)dp, (unsigned)optlen); 1080 dp += optlen; 1081 } 1082 } 1083 1084 for (optlen = dp - (u_char *)(ipdst+1); optlen & 0x3; optlen++) { 1085 *dp++ = IPOPT_EOL; 1086 } 1087 1088 return optlen; 1089 } 1090 1091 /* 1092 * IP socket option processing. 1093 */ 1094 int 1095 ip_ctloutput(int op, struct socket *so, struct sockopt *sopt) 1096 { 1097 struct inpcb *inp = sotoinpcb(so); 1098 struct ip *ip = &in4p_ip(inp); 1099 int inpflags = inp->inp_flags; 1100 int optval = 0, error = 0; 1101 struct in_pktinfo pktinfo; 1102 1103 KASSERT(solocked(so)); 1104 1105 if (sopt->sopt_level != IPPROTO_IP) { 1106 if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) 1107 return 0; 1108 return ENOPROTOOPT; 1109 } 1110 1111 switch (op) { 1112 case PRCO_SETOPT: 1113 switch (sopt->sopt_name) { 1114 case IP_OPTIONS: 1115 #ifdef notyet 1116 case IP_RETOPTS: 1117 #endif 1118 error = ip_pcbopts(inp, sopt); 1119 break; 1120 1121 case IP_TOS: 1122 case IP_TTL: 1123 case IP_MINTTL: 1124 case IP_RECVOPTS: 1125 case IP_RECVRETOPTS: 1126 case IP_RECVDSTADDR: 1127 case IP_RECVIF: 1128 case IP_RECVPKTINFO: 1129 case IP_RECVTTL: 1130 case IP_BINDANY: 1131 error = sockopt_getint(sopt, &optval); 1132 if (error) 1133 break; 1134 1135 switch (sopt->sopt_name) { 1136 case IP_TOS: 1137 ip->ip_tos = optval; 1138 break; 1139 1140 case IP_TTL: 1141 ip->ip_ttl = optval; 1142 break; 1143 1144 case IP_MINTTL: 1145 if (optval > 0 && optval <= MAXTTL) 1146 in4p_ip_minttl(inp) = optval; 1147 else 1148 error = EINVAL; 1149 break; 1150 #define OPTSET(bit) \ 1151 if (optval) \ 1152 inpflags |= bit; \ 1153 else \ 1154 inpflags &= ~bit; 1155 1156 case IP_RECVOPTS: 1157 OPTSET(INP_RECVOPTS); 1158 break; 1159 1160 case IP_RECVPKTINFO: 1161 OPTSET(INP_RECVPKTINFO); 1162 break; 1163 1164 case IP_RECVRETOPTS: 1165 OPTSET(INP_RECVRETOPTS); 1166 break; 1167 1168 case IP_RECVDSTADDR: 1169 OPTSET(INP_RECVDSTADDR); 1170 break; 1171 1172 case IP_RECVIF: 1173 OPTSET(INP_RECVIF); 1174 break; 1175 1176 case IP_RECVTTL: 1177 OPTSET(INP_RECVTTL); 1178 break; 1179 1180 case IP_BINDANY: 1181 error = kauth_authorize_network( 1182 kauth_cred_get(), KAUTH_NETWORK_BIND, 1183 KAUTH_REQ_NETWORK_BIND_ANYADDR, so, 1184 NULL, NULL); 1185 if (error == 0) { 1186 OPTSET(INP_BINDANY); 1187 } 1188 break; 1189 } 1190 break; 1191 case IP_PKTINFO: 1192 error = sockopt_getint(sopt, &optval); 1193 if (!error) { 1194 /* Linux compatibility */ 1195 OPTSET(INP_RECVPKTINFO); 1196 break; 1197 } 1198 error = sockopt_get(sopt, &pktinfo, sizeof(pktinfo)); 1199 if (error) 1200 break; 1201 1202 if (pktinfo.ipi_ifindex == 0) { 1203 in4p_prefsrcip(inp) = pktinfo.ipi_addr; 1204 break; 1205 } 1206 1207 /* Solaris compatibility */ 1208 struct ifnet *ifp; 1209 struct in_ifaddr *ia; 1210 int s; 1211 1212 /* pick up primary address */ 1213 s = pserialize_read_enter(); 1214 ifp = if_byindex(pktinfo.ipi_ifindex); 1215 if (ifp == NULL) { 1216 pserialize_read_exit(s); 1217 error = EADDRNOTAVAIL; 1218 break; 1219 } 1220 ia = in_get_ia_from_ifp(ifp); 1221 if (ia == NULL) { 1222 pserialize_read_exit(s); 1223 error = EADDRNOTAVAIL; 1224 break; 1225 } 1226 in4p_prefsrcip(inp) = IA_SIN(ia)->sin_addr; 1227 pserialize_read_exit(s); 1228 break; 1229 break; 1230 #undef OPTSET 1231 1232 case IP_MULTICAST_IF: 1233 case IP_MULTICAST_TTL: 1234 case IP_MULTICAST_LOOP: 1235 case IP_ADD_MEMBERSHIP: 1236 case IP_DROP_MEMBERSHIP: 1237 error = ip_setmoptions(&inp->inp_moptions, sopt); 1238 break; 1239 1240 case IP_PORTRANGE: 1241 error = sockopt_getint(sopt, &optval); 1242 if (error) 1243 break; 1244 1245 switch (optval) { 1246 case IP_PORTRANGE_DEFAULT: 1247 case IP_PORTRANGE_HIGH: 1248 inpflags &= ~(INP_LOWPORT); 1249 break; 1250 1251 case IP_PORTRANGE_LOW: 1252 inpflags |= INP_LOWPORT; 1253 break; 1254 1255 default: 1256 error = EINVAL; 1257 break; 1258 } 1259 break; 1260 1261 case IP_PORTALGO: 1262 error = sockopt_getint(sopt, &optval); 1263 if (error) 1264 break; 1265 1266 error = portalgo_algo_index_select(inp, optval); 1267 break; 1268 1269 #if defined(IPSEC) 1270 case IP_IPSEC_POLICY: 1271 if (ipsec_enabled) { 1272 error = ipsec_set_policy(inp, 1273 sopt->sopt_data, sopt->sopt_size, 1274 curlwp->l_cred); 1275 } else 1276 error = ENOPROTOOPT; 1277 break; 1278 #endif /* IPSEC */ 1279 1280 default: 1281 error = ENOPROTOOPT; 1282 break; 1283 } 1284 break; 1285 1286 case PRCO_GETOPT: 1287 switch (sopt->sopt_name) { 1288 case IP_OPTIONS: 1289 case IP_RETOPTS: { 1290 struct mbuf *mopts = inp->inp_options; 1291 1292 if (mopts) { 1293 struct mbuf *m; 1294 1295 m = m_copym(mopts, 0, M_COPYALL, M_DONTWAIT); 1296 if (m == NULL) { 1297 error = ENOBUFS; 1298 break; 1299 } 1300 error = sockopt_setmbuf(sopt, m); 1301 } 1302 break; 1303 } 1304 case IP_TOS: 1305 case IP_TTL: 1306 case IP_MINTTL: 1307 case IP_RECVOPTS: 1308 case IP_RECVRETOPTS: 1309 case IP_RECVDSTADDR: 1310 case IP_RECVIF: 1311 case IP_RECVPKTINFO: 1312 case IP_RECVTTL: 1313 case IP_ERRORMTU: 1314 case IP_BINDANY: 1315 switch (sopt->sopt_name) { 1316 case IP_TOS: 1317 optval = ip->ip_tos; 1318 break; 1319 1320 case IP_TTL: 1321 optval = ip->ip_ttl; 1322 break; 1323 1324 case IP_MINTTL: 1325 optval = in4p_ip_minttl(inp); 1326 break; 1327 1328 case IP_ERRORMTU: 1329 optval = in4p_errormtu(inp); 1330 break; 1331 1332 #define OPTBIT(bit) (inpflags & bit ? 1 : 0) 1333 1334 case IP_RECVOPTS: 1335 optval = OPTBIT(INP_RECVOPTS); 1336 break; 1337 1338 case IP_RECVPKTINFO: 1339 optval = OPTBIT(INP_RECVPKTINFO); 1340 break; 1341 1342 case IP_RECVRETOPTS: 1343 optval = OPTBIT(INP_RECVRETOPTS); 1344 break; 1345 1346 case IP_RECVDSTADDR: 1347 optval = OPTBIT(INP_RECVDSTADDR); 1348 break; 1349 1350 case IP_RECVIF: 1351 optval = OPTBIT(INP_RECVIF); 1352 break; 1353 1354 case IP_RECVTTL: 1355 optval = OPTBIT(INP_RECVTTL); 1356 break; 1357 1358 case IP_BINDANY: 1359 optval = OPTBIT(INP_BINDANY); 1360 break; 1361 } 1362 error = sockopt_setint(sopt, optval); 1363 break; 1364 1365 case IP_PKTINFO: 1366 switch (sopt->sopt_size) { 1367 case sizeof(int): 1368 /* Linux compatibility */ 1369 optval = OPTBIT(INP_RECVPKTINFO); 1370 error = sockopt_setint(sopt, optval); 1371 break; 1372 case sizeof(struct in_pktinfo): 1373 /* Solaris compatibility */ 1374 pktinfo.ipi_ifindex = 0; 1375 pktinfo.ipi_addr = in4p_prefsrcip(inp); 1376 error = sockopt_set(sopt, &pktinfo, 1377 sizeof(pktinfo)); 1378 break; 1379 default: 1380 /* 1381 * While size is stuck at 0, and, later, if 1382 * the caller doesn't use an exactly sized 1383 * recipient for the data, default to Linux 1384 * compatibility 1385 */ 1386 optval = OPTBIT(INP_RECVPKTINFO); 1387 error = sockopt_setint(sopt, optval); 1388 break; 1389 } 1390 break; 1391 1392 #if 0 /* defined(IPSEC) */ 1393 case IP_IPSEC_POLICY: 1394 { 1395 struct mbuf *m = NULL; 1396 1397 /* XXX this will return EINVAL as sopt is empty */ 1398 error = ipsec_get_policy(inp, sopt->sopt_data, 1399 sopt->sopt_size, &m); 1400 if (error == 0) 1401 error = sockopt_setmbuf(sopt, m); 1402 break; 1403 } 1404 #endif /*IPSEC*/ 1405 1406 case IP_MULTICAST_IF: 1407 case IP_MULTICAST_TTL: 1408 case IP_MULTICAST_LOOP: 1409 case IP_ADD_MEMBERSHIP: 1410 case IP_DROP_MEMBERSHIP: 1411 error = ip_getmoptions(inp->inp_moptions, sopt); 1412 break; 1413 1414 case IP_PORTRANGE: 1415 if (inpflags & INP_LOWPORT) 1416 optval = IP_PORTRANGE_LOW; 1417 else 1418 optval = IP_PORTRANGE_DEFAULT; 1419 error = sockopt_setint(sopt, optval); 1420 break; 1421 1422 case IP_PORTALGO: 1423 optval = inp->inp_portalgo; 1424 error = sockopt_setint(sopt, optval); 1425 break; 1426 1427 default: 1428 error = ENOPROTOOPT; 1429 break; 1430 } 1431 break; 1432 } 1433 1434 if (!error) { 1435 inp->inp_flags = inpflags; 1436 } 1437 return error; 1438 } 1439 1440 static int 1441 ip_pktinfo_prepare(const struct inpcb *inp, const struct in_pktinfo *pktinfo, 1442 struct ip_pktopts *pktopts, int *flags, kauth_cred_t cred) 1443 { 1444 struct ip_moptions *imo; 1445 int error = 0; 1446 bool addrset = false; 1447 1448 if (!in_nullhost(pktinfo->ipi_addr)) { 1449 pktopts->ippo_laddr.sin_addr = pktinfo->ipi_addr; 1450 /* EADDRNOTAVAIL? */ 1451 error = inpcb_bindableaddr(inp, &pktopts->ippo_laddr, cred); 1452 if (error != 0) 1453 return error; 1454 addrset = true; 1455 } 1456 1457 if (pktinfo->ipi_ifindex != 0) { 1458 if (!addrset) { 1459 struct ifnet *ifp; 1460 struct in_ifaddr *ia; 1461 int s; 1462 1463 /* pick up primary address */ 1464 s = pserialize_read_enter(); 1465 ifp = if_byindex(pktinfo->ipi_ifindex); 1466 if (ifp == NULL) { 1467 pserialize_read_exit(s); 1468 return EADDRNOTAVAIL; 1469 } 1470 ia = in_get_ia_from_ifp(ifp); 1471 if (ia == NULL) { 1472 pserialize_read_exit(s); 1473 return EADDRNOTAVAIL; 1474 } 1475 pktopts->ippo_laddr.sin_addr = IA_SIN(ia)->sin_addr; 1476 pserialize_read_exit(s); 1477 } 1478 1479 /* 1480 * If specified ipi_ifindex, 1481 * use copied or locally initialized ip_moptions. 1482 * Original ip_moptions must not be modified. 1483 */ 1484 imo = &pktopts->ippo_imobuf; /* local buf in pktopts */ 1485 if (pktopts->ippo_imo != NULL) { 1486 memcpy(imo, pktopts->ippo_imo, sizeof(*imo)); 1487 } else { 1488 memset(imo, 0, sizeof(*imo)); 1489 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1490 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1491 } 1492 imo->imo_multicast_if_index = pktinfo->ipi_ifindex; 1493 pktopts->ippo_imo = imo; 1494 *flags |= IP_ROUTETOIFINDEX; 1495 } 1496 return error; 1497 } 1498 1499 /* 1500 * Set up IP outgoing packet options. Even if control is NULL, 1501 * pktopts->ippo_laddr and pktopts->ippo_imo are set and used. 1502 */ 1503 int 1504 ip_setpktopts(struct mbuf *control, struct ip_pktopts *pktopts, int *flags, 1505 struct inpcb *inp, kauth_cred_t cred) 1506 { 1507 struct cmsghdr *cm; 1508 struct in_pktinfo pktinfo; 1509 int error; 1510 1511 pktopts->ippo_imo = inp->inp_moptions; 1512 1513 struct in_addr *ia = in_nullhost(in4p_prefsrcip(inp)) ? &in4p_laddr(inp) : 1514 &in4p_prefsrcip(inp); 1515 sockaddr_in_init(&pktopts->ippo_laddr, ia, 0); 1516 1517 if (control == NULL) 1518 return 0; 1519 1520 /* 1521 * XXX: Currently, we assume all the optional information is 1522 * stored in a single mbuf. 1523 */ 1524 if (control->m_next) 1525 return EINVAL; 1526 1527 for (; control->m_len > 0; 1528 control->m_data += CMSG_ALIGN(cm->cmsg_len), 1529 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 1530 cm = mtod(control, struct cmsghdr *); 1531 if ((control->m_len < sizeof(*cm)) || 1532 (cm->cmsg_len == 0) || 1533 (cm->cmsg_len > control->m_len)) { 1534 return EINVAL; 1535 } 1536 if (cm->cmsg_level != IPPROTO_IP) 1537 continue; 1538 1539 switch (cm->cmsg_type) { 1540 case IP_PKTINFO: 1541 if (cm->cmsg_len != CMSG_LEN(sizeof(pktinfo))) 1542 return EINVAL; 1543 memcpy(&pktinfo, CMSG_DATA(cm), sizeof(pktinfo)); 1544 error = ip_pktinfo_prepare(inp, &pktinfo, pktopts, 1545 flags, cred); 1546 if (error) 1547 return error; 1548 break; 1549 case IP_SENDSRCADDR: /* FreeBSD compatibility */ 1550 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in_addr))) 1551 return EINVAL; 1552 pktinfo.ipi_ifindex = 0; 1553 pktinfo.ipi_addr = 1554 ((struct in_pktinfo *)CMSG_DATA(cm))->ipi_addr; 1555 error = ip_pktinfo_prepare(inp, &pktinfo, pktopts, 1556 flags, cred); 1557 if (error) 1558 return error; 1559 break; 1560 default: 1561 return ENOPROTOOPT; 1562 } 1563 } 1564 return 0; 1565 } 1566 1567 /* 1568 * Set up IP options in pcb for insertion in output packets. 1569 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1570 * with destination address if source routed. 1571 */ 1572 static int 1573 ip_pcbopts(struct inpcb *inp, const struct sockopt *sopt) 1574 { 1575 struct mbuf *m; 1576 const u_char *cp; 1577 u_char *dp; 1578 int cnt; 1579 1580 KASSERT(inp_locked(inp)); 1581 1582 /* Turn off any old options. */ 1583 if (inp->inp_options) { 1584 m_free(inp->inp_options); 1585 } 1586 inp->inp_options = NULL; 1587 if ((cnt = sopt->sopt_size) == 0) { 1588 /* Only turning off any previous options. */ 1589 return 0; 1590 } 1591 cp = sopt->sopt_data; 1592 1593 if (cnt % 4) { 1594 /* Must be 4-byte aligned, because there's no padding. */ 1595 return EINVAL; 1596 } 1597 1598 m = m_get(M_DONTWAIT, MT_SOOPTS); 1599 if (m == NULL) 1600 return ENOBUFS; 1601 1602 dp = mtod(m, u_char *); 1603 memset(dp, 0, sizeof(struct in_addr)); 1604 dp += sizeof(struct in_addr); 1605 m->m_len = sizeof(struct in_addr); 1606 1607 /* 1608 * IP option list according to RFC791. Each option is of the form 1609 * 1610 * [optval] [olen] [(olen - 2) data bytes] 1611 * 1612 * We validate the list and copy options to an mbuf for prepending 1613 * to data packets. The IP first-hop destination address will be 1614 * stored before actual options and is zero if unset. 1615 */ 1616 while (cnt > 0) { 1617 uint8_t optval, olen, offset; 1618 1619 optval = cp[IPOPT_OPTVAL]; 1620 1621 if (optval == IPOPT_EOL || optval == IPOPT_NOP) { 1622 olen = 1; 1623 } else { 1624 if (cnt < IPOPT_OLEN + 1) 1625 goto bad; 1626 1627 olen = cp[IPOPT_OLEN]; 1628 if (olen < IPOPT_OLEN + 1 || olen > cnt) 1629 goto bad; 1630 } 1631 1632 if (optval == IPOPT_LSRR || optval == IPOPT_SSRR) { 1633 /* 1634 * user process specifies route as: 1635 * ->A->B->C->D 1636 * D must be our final destination (but we can't 1637 * check that since we may not have connected yet). 1638 * A is first hop destination, which doesn't appear in 1639 * actual IP option, but is stored before the options. 1640 */ 1641 if (olen < IPOPT_OFFSET + 1 + sizeof(struct in_addr)) 1642 goto bad; 1643 1644 offset = cp[IPOPT_OFFSET]; 1645 memcpy(mtod(m, u_char *), cp + IPOPT_OFFSET + 1, 1646 sizeof(struct in_addr)); 1647 1648 cp += sizeof(struct in_addr); 1649 cnt -= sizeof(struct in_addr); 1650 olen -= sizeof(struct in_addr); 1651 1652 if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr)) 1653 goto bad; 1654 1655 memcpy(dp, cp, olen); 1656 dp[IPOPT_OPTVAL] = optval; 1657 dp[IPOPT_OLEN] = olen; 1658 dp[IPOPT_OFFSET] = offset; 1659 break; 1660 } else { 1661 if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr)) 1662 goto bad; 1663 1664 memcpy(dp, cp, olen); 1665 break; 1666 } 1667 1668 dp += olen; 1669 m->m_len += olen; 1670 1671 if (optval == IPOPT_EOL) 1672 break; 1673 1674 cp += olen; 1675 cnt -= olen; 1676 } 1677 1678 inp->inp_options = m; 1679 return 0; 1680 1681 bad: 1682 (void)m_free(m); 1683 return EINVAL; 1684 } 1685 1686 /* 1687 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1688 * Must be called in a pserialize critical section. 1689 */ 1690 static struct ifnet * 1691 ip_multicast_if(struct in_addr *a, int *ifindexp) 1692 { 1693 int ifindex; 1694 struct ifnet *ifp = NULL; 1695 struct in_ifaddr *ia; 1696 1697 if (ifindexp) 1698 *ifindexp = 0; 1699 if (ntohl(a->s_addr) >> 24 == 0) { 1700 ifindex = ntohl(a->s_addr) & 0xffffff; 1701 ifp = if_byindex(ifindex); 1702 if (!ifp) 1703 return NULL; 1704 if (ifindexp) 1705 *ifindexp = ifindex; 1706 } else { 1707 IN_ADDRHASH_READER_FOREACH(ia, a->s_addr) { 1708 if (in_hosteq(ia->ia_addr.sin_addr, *a) && 1709 (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) { 1710 ifp = ia->ia_ifp; 1711 if (if_is_deactivated(ifp)) 1712 ifp = NULL; 1713 break; 1714 } 1715 } 1716 } 1717 return ifp; 1718 } 1719 1720 static int 1721 ip_getoptval(const struct sockopt *sopt, u_int8_t *val, u_int maxval) 1722 { 1723 u_int tval; 1724 u_char cval; 1725 int error; 1726 1727 if (sopt == NULL) 1728 return EINVAL; 1729 1730 switch (sopt->sopt_size) { 1731 case sizeof(u_char): 1732 error = sockopt_get(sopt, &cval, sizeof(u_char)); 1733 tval = cval; 1734 break; 1735 1736 case sizeof(u_int): 1737 error = sockopt_get(sopt, &tval, sizeof(u_int)); 1738 break; 1739 1740 default: 1741 error = EINVAL; 1742 } 1743 1744 if (error) 1745 return error; 1746 1747 if (tval > maxval) 1748 return EINVAL; 1749 1750 *val = tval; 1751 return 0; 1752 } 1753 1754 static int 1755 ip_get_membership(const struct sockopt *sopt, struct ifnet **ifp, 1756 struct psref *psref, struct in_addr *ia, bool add) 1757 { 1758 int error; 1759 struct ip_mreq mreq; 1760 1761 error = sockopt_get(sopt, &mreq, sizeof(mreq)); 1762 if (error) 1763 return error; 1764 1765 if (!IN_MULTICAST(mreq.imr_multiaddr.s_addr)) 1766 return EINVAL; 1767 1768 memcpy(ia, &mreq.imr_multiaddr, sizeof(*ia)); 1769 1770 if (in_nullhost(mreq.imr_interface)) { 1771 union { 1772 struct sockaddr dst; 1773 struct sockaddr_in dst4; 1774 } u; 1775 struct route ro; 1776 1777 if (!add) { 1778 *ifp = NULL; 1779 return 0; 1780 } 1781 /* 1782 * If no interface address was provided, use the interface of 1783 * the route to the given multicast address. 1784 */ 1785 struct rtentry *rt; 1786 memset(&ro, 0, sizeof(ro)); 1787 1788 sockaddr_in_init(&u.dst4, ia, 0); 1789 error = rtcache_setdst(&ro, &u.dst); 1790 if (error != 0) 1791 return error; 1792 *ifp = (rt = rtcache_init(&ro)) != NULL ? rt->rt_ifp : NULL; 1793 if (*ifp != NULL) { 1794 if (if_is_deactivated(*ifp)) 1795 *ifp = NULL; 1796 else 1797 if_acquire(*ifp, psref); 1798 } 1799 rtcache_unref(rt, &ro); 1800 rtcache_free(&ro); 1801 } else { 1802 int s = pserialize_read_enter(); 1803 *ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1804 if (!add && *ifp == NULL) { 1805 pserialize_read_exit(s); 1806 return EADDRNOTAVAIL; 1807 } 1808 if (*ifp != NULL) { 1809 if (if_is_deactivated(*ifp)) 1810 *ifp = NULL; 1811 else 1812 if_acquire(*ifp, psref); 1813 } 1814 pserialize_read_exit(s); 1815 } 1816 return 0; 1817 } 1818 1819 /* 1820 * Add a multicast group membership. 1821 * Group must be a valid IP multicast address. 1822 */ 1823 static int 1824 ip_add_membership(struct ip_moptions *imo, const struct sockopt *sopt) 1825 { 1826 struct ifnet *ifp = NULL; // XXX: gcc [ppc] 1827 struct in_addr ia; 1828 int i, error, bound; 1829 struct psref psref; 1830 1831 /* imo is protected by solock or referenced only by the caller */ 1832 1833 bound = curlwp_bind(); 1834 if (sopt->sopt_size == sizeof(struct ip_mreq)) 1835 error = ip_get_membership(sopt, &ifp, &psref, &ia, true); 1836 else { 1837 #ifdef INET6 1838 error = ip6_get_membership(sopt, &ifp, &psref, &ia, sizeof(ia)); 1839 #else 1840 error = EINVAL; 1841 #endif 1842 } 1843 1844 if (error) 1845 goto out; 1846 1847 /* 1848 * See if we found an interface, and confirm that it 1849 * supports multicast. 1850 */ 1851 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1852 error = EADDRNOTAVAIL; 1853 goto out; 1854 } 1855 1856 /* 1857 * See if the membership already exists or if all the 1858 * membership slots are full. 1859 */ 1860 for (i = 0; i < imo->imo_num_memberships; ++i) { 1861 if (imo->imo_membership[i]->inm_ifp == ifp && 1862 in_hosteq(imo->imo_membership[i]->inm_addr, ia)) 1863 break; 1864 } 1865 if (i < imo->imo_num_memberships) { 1866 error = EADDRINUSE; 1867 goto out; 1868 } 1869 1870 if (i == IP_MAX_MEMBERSHIPS) { 1871 error = ETOOMANYREFS; 1872 goto out; 1873 } 1874 1875 /* 1876 * Everything looks good; add a new record to the multicast 1877 * address list for the given interface. 1878 */ 1879 imo->imo_membership[i] = in_addmulti(&ia, ifp); 1880 if (imo->imo_membership[i] == NULL) { 1881 error = ENOBUFS; 1882 goto out; 1883 } 1884 1885 ++imo->imo_num_memberships; 1886 error = 0; 1887 out: 1888 if_put(ifp, &psref); 1889 curlwp_bindx(bound); 1890 return error; 1891 } 1892 1893 /* 1894 * Drop a multicast group membership. 1895 * Group must be a valid IP multicast address. 1896 */ 1897 static int 1898 ip_drop_membership(struct ip_moptions *imo, const struct sockopt *sopt) 1899 { 1900 struct in_addr ia = { .s_addr = 0 }; // XXX: gcc [ppc] 1901 struct ifnet *ifp = NULL; // XXX: gcc [ppc] 1902 int i, error, bound; 1903 struct psref psref; 1904 1905 /* imo is protected by solock or referenced only by the caller */ 1906 1907 bound = curlwp_bind(); 1908 if (sopt->sopt_size == sizeof(struct ip_mreq)) 1909 error = ip_get_membership(sopt, &ifp, &psref, &ia, false); 1910 else { 1911 #ifdef INET6 1912 error = ip6_get_membership(sopt, &ifp, &psref, &ia, sizeof(ia)); 1913 #else 1914 error = EINVAL; 1915 #endif 1916 } 1917 1918 if (error) 1919 goto out; 1920 1921 /* 1922 * Find the membership in the membership array. 1923 */ 1924 for (i = 0; i < imo->imo_num_memberships; ++i) { 1925 if ((ifp == NULL || 1926 imo->imo_membership[i]->inm_ifp == ifp) && 1927 in_hosteq(imo->imo_membership[i]->inm_addr, ia)) 1928 break; 1929 } 1930 if (i == imo->imo_num_memberships) { 1931 error = EADDRNOTAVAIL; 1932 goto out; 1933 } 1934 1935 /* 1936 * Give up the multicast address record to which the 1937 * membership points. 1938 */ 1939 in_delmulti(imo->imo_membership[i]); 1940 1941 /* 1942 * Remove the gap in the membership array. 1943 */ 1944 for (++i; i < imo->imo_num_memberships; ++i) 1945 imo->imo_membership[i-1] = imo->imo_membership[i]; 1946 --imo->imo_num_memberships; 1947 error = 0; 1948 out: 1949 if_put(ifp, &psref); 1950 curlwp_bindx(bound); 1951 return error; 1952 } 1953 1954 /* 1955 * Set the IP multicast options in response to user setsockopt(). 1956 */ 1957 int 1958 ip_setmoptions(struct ip_moptions **pimo, const struct sockopt *sopt) 1959 { 1960 struct ip_moptions *imo = *pimo; 1961 struct in_addr addr; 1962 struct ifnet *ifp; 1963 int ifindex, error = 0; 1964 1965 /* The passed imo isn't NULL, it should be protected by solock */ 1966 1967 if (!imo) { 1968 /* 1969 * No multicast option buffer attached to the pcb; 1970 * allocate one and initialize to default values. 1971 */ 1972 imo = kmem_intr_alloc(sizeof(*imo), KM_NOSLEEP); 1973 if (imo == NULL) 1974 return ENOBUFS; 1975 1976 imo->imo_multicast_if_index = 0; 1977 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1978 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1979 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1980 imo->imo_num_memberships = 0; 1981 *pimo = imo; 1982 } 1983 1984 switch (sopt->sopt_name) { 1985 case IP_MULTICAST_IF: { 1986 int s; 1987 /* 1988 * Select the interface for outgoing multicast packets. 1989 */ 1990 error = sockopt_get(sopt, &addr, sizeof(addr)); 1991 if (error) 1992 break; 1993 1994 /* 1995 * INADDR_ANY is used to remove a previous selection. 1996 * When no interface is selected, a default one is 1997 * chosen every time a multicast packet is sent. 1998 */ 1999 if (in_nullhost(addr)) { 2000 imo->imo_multicast_if_index = 0; 2001 break; 2002 } 2003 /* 2004 * The selected interface is identified by its local 2005 * IP address. Find the interface and confirm that 2006 * it supports multicasting. 2007 */ 2008 s = pserialize_read_enter(); 2009 ifp = ip_multicast_if(&addr, &ifindex); 2010 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2011 pserialize_read_exit(s); 2012 error = EADDRNOTAVAIL; 2013 break; 2014 } 2015 imo->imo_multicast_if_index = ifp->if_index; 2016 pserialize_read_exit(s); 2017 if (ifindex) 2018 imo->imo_multicast_addr = addr; 2019 else 2020 imo->imo_multicast_addr.s_addr = INADDR_ANY; 2021 break; 2022 } 2023 2024 case IP_MULTICAST_TTL: 2025 /* 2026 * Set the IP time-to-live for outgoing multicast packets. 2027 */ 2028 error = ip_getoptval(sopt, &imo->imo_multicast_ttl, MAXTTL); 2029 break; 2030 2031 case IP_MULTICAST_LOOP: 2032 /* 2033 * Set the loopback flag for outgoing multicast packets. 2034 * Must be zero or one. 2035 */ 2036 error = ip_getoptval(sopt, &imo->imo_multicast_loop, 1); 2037 break; 2038 2039 case IP_ADD_MEMBERSHIP: /* IPV6_JOIN_GROUP */ 2040 error = ip_add_membership(imo, sopt); 2041 break; 2042 2043 case IP_DROP_MEMBERSHIP: /* IPV6_LEAVE_GROUP */ 2044 error = ip_drop_membership(imo, sopt); 2045 break; 2046 2047 default: 2048 error = EOPNOTSUPP; 2049 break; 2050 } 2051 2052 /* 2053 * If all options have default values, no need to keep the mbuf. 2054 */ 2055 if (imo->imo_multicast_if_index == 0 && 2056 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 2057 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 2058 imo->imo_num_memberships == 0) { 2059 kmem_intr_free(imo, sizeof(*imo)); 2060 *pimo = NULL; 2061 } 2062 2063 return error; 2064 } 2065 2066 /* 2067 * Return the IP multicast options in response to user getsockopt(). 2068 */ 2069 int 2070 ip_getmoptions(struct ip_moptions *imo, struct sockopt *sopt) 2071 { 2072 struct in_addr addr; 2073 uint8_t optval; 2074 int error = 0; 2075 2076 /* imo is protected by solock or referenced only by the caller */ 2077 2078 switch (sopt->sopt_name) { 2079 case IP_MULTICAST_IF: 2080 if (imo == NULL || imo->imo_multicast_if_index == 0) 2081 addr = zeroin_addr; 2082 else if (imo->imo_multicast_addr.s_addr) { 2083 /* return the value user has set */ 2084 addr = imo->imo_multicast_addr; 2085 } else { 2086 struct ifnet *ifp; 2087 struct in_ifaddr *ia = NULL; 2088 int s = pserialize_read_enter(); 2089 2090 ifp = if_byindex(imo->imo_multicast_if_index); 2091 if (ifp != NULL) { 2092 ia = in_get_ia_from_ifp(ifp); 2093 } 2094 addr = ia ? ia->ia_addr.sin_addr : zeroin_addr; 2095 pserialize_read_exit(s); 2096 } 2097 error = sockopt_set(sopt, &addr, sizeof(addr)); 2098 break; 2099 2100 case IP_MULTICAST_TTL: 2101 optval = imo ? imo->imo_multicast_ttl 2102 : IP_DEFAULT_MULTICAST_TTL; 2103 2104 error = sockopt_set(sopt, &optval, sizeof(optval)); 2105 break; 2106 2107 case IP_MULTICAST_LOOP: 2108 optval = imo ? imo->imo_multicast_loop 2109 : IP_DEFAULT_MULTICAST_LOOP; 2110 2111 error = sockopt_set(sopt, &optval, sizeof(optval)); 2112 break; 2113 2114 default: 2115 error = EOPNOTSUPP; 2116 } 2117 2118 return error; 2119 } 2120 2121 /* 2122 * Discard the IP multicast options. 2123 */ 2124 void 2125 ip_freemoptions(struct ip_moptions *imo) 2126 { 2127 int i; 2128 2129 /* The owner of imo (inp) should be protected by solock */ 2130 2131 if (imo != NULL) { 2132 for (i = 0; i < imo->imo_num_memberships; ++i) { 2133 struct in_multi *inm = imo->imo_membership[i]; 2134 in_delmulti(inm); 2135 /* ifp should not leave thanks to solock */ 2136 } 2137 2138 kmem_intr_free(imo, sizeof(*imo)); 2139 } 2140 } 2141 2142 /* 2143 * Routine called from ip_output() to loop back a copy of an IP multicast 2144 * packet to the input queue of a specified interface. Note that this 2145 * calls the output routine of the loopback "driver", but with an interface 2146 * pointer that might NOT be lo0ifp -- easier than replicating that code here. 2147 */ 2148 static void 2149 ip_mloopback(struct ifnet *ifp, struct mbuf *m, const struct sockaddr_in *dst) 2150 { 2151 struct ip *ip; 2152 struct mbuf *copym; 2153 2154 copym = m_copypacket(m, M_DONTWAIT); 2155 if (copym != NULL && 2156 (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip))) 2157 copym = m_pullup(copym, sizeof(struct ip)); 2158 if (copym == NULL) 2159 return; 2160 /* 2161 * We don't bother to fragment if the IP length is greater 2162 * than the interface's MTU. Can this possibly matter? 2163 */ 2164 ip = mtod(copym, struct ip *); 2165 2166 if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 2167 in_undefer_cksum_tcpudp(copym); 2168 copym->m_pkthdr.csum_flags &= 2169 ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 2170 } 2171 2172 ip->ip_sum = 0; 2173 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2); 2174 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 2175 (void)looutput(ifp, copym, sintocsa(dst), NULL); 2176 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 2177 } 2178 2179 /* 2180 * Ensure sending address is valid. 2181 * Returns 0 on success, -1 if an error should be sent back or 1 2182 * if the packet could be dropped without error (protocol dependent). 2183 */ 2184 static int 2185 ip_ifaddrvalid(const struct in_ifaddr *ia) 2186 { 2187 2188 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY) 2189 return 0; 2190 2191 if (ia->ia4_flags & IN_IFF_DUPLICATED) 2192 return -1; 2193 else if (ia->ia4_flags & (IN_IFF_TENTATIVE | IN_IFF_DETACHED)) 2194 return 1; 2195 2196 return 0; 2197 } 2198