1 /* $NetBSD: ip_output.c,v 1.270 2017/02/13 04:06: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.270 2017/02/13 04:06: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 131 #ifdef INET6 132 #include <netinet6/ip6_var.h> 133 #endif 134 135 #ifdef MROUTING 136 #include <netinet/ip_mroute.h> 137 #endif 138 139 #ifdef IPSEC 140 #include <netipsec/ipsec.h> 141 #include <netipsec/key.h> 142 #endif 143 144 #ifdef MPLS 145 #include <netmpls/mpls.h> 146 #include <netmpls/mpls_var.h> 147 #endif 148 149 static int ip_pcbopts(struct inpcb *, const struct sockopt *); 150 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *); 151 static struct ifnet *ip_multicast_if(struct in_addr *, int *); 152 static void ip_mloopback(struct ifnet *, struct mbuf *, 153 const struct sockaddr_in *); 154 static int ip_ifaddrvalid(const struct in_ifaddr *); 155 156 extern pfil_head_t *inet_pfil_hook; /* XXX */ 157 158 int ip_do_loopback_cksum = 0; 159 160 static int 161 ip_mark_mpls(struct ifnet * const ifp, struct mbuf * const m, 162 const struct rtentry *rt) 163 { 164 int error = 0; 165 #ifdef MPLS 166 union mpls_shim msh; 167 168 if (rt == NULL || rt_gettag(rt) == NULL || 169 rt_gettag(rt)->sa_family != AF_MPLS || 170 (m->m_flags & (M_MCAST | M_BCAST)) != 0 || 171 ifp->if_type != IFT_ETHER) 172 return 0; 173 174 msh.s_addr = MPLS_GETSADDR(rt); 175 if (msh.shim.label != MPLS_LABEL_IMPLNULL) { 176 struct m_tag *mtag; 177 /* 178 * XXX tentative solution to tell ether_output 179 * it's MPLS. Need some more efficient solution. 180 */ 181 mtag = m_tag_get(PACKET_TAG_MPLS, 182 sizeof(int) /* dummy */, 183 M_NOWAIT); 184 if (mtag == NULL) 185 return ENOMEM; 186 m_tag_prepend(m, mtag); 187 } 188 #endif 189 return error; 190 } 191 192 /* 193 * Send an IP packet to a host. 194 */ 195 int 196 ip_if_output(struct ifnet * const ifp, struct mbuf * const m, 197 const struct sockaddr * const dst, const struct rtentry *rt) 198 { 199 int error = 0; 200 201 if (rt != NULL) { 202 error = rt_check_reject_route(rt, ifp); 203 if (error != 0) { 204 m_freem(m); 205 return error; 206 } 207 } 208 209 error = ip_mark_mpls(ifp, m, rt); 210 if (error != 0) { 211 m_freem(m); 212 return error; 213 } 214 215 error = if_output_lock(ifp, ifp, m, dst, rt); 216 217 return error; 218 } 219 220 /* 221 * IP output. The packet in mbuf chain m contains a skeletal IP 222 * header (with len, off, ttl, proto, tos, src, dst). 223 * The mbuf chain containing the packet will be freed. 224 * The mbuf opt, if present, will not be freed. 225 */ 226 int 227 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, int flags, 228 struct ip_moptions *imo, struct socket *so) 229 { 230 struct rtentry *rt; 231 struct ip *ip; 232 struct ifnet *ifp, *mifp = NULL; 233 struct mbuf *m = m0; 234 int hlen = sizeof (struct ip); 235 int len, error = 0; 236 struct route iproute; 237 const struct sockaddr_in *dst; 238 struct in_ifaddr *ia = NULL; 239 int isbroadcast; 240 int sw_csum; 241 u_long mtu; 242 #ifdef IPSEC 243 struct secpolicy *sp = NULL; 244 #endif 245 bool natt_frag = false; 246 bool rtmtu_nolock; 247 union { 248 struct sockaddr dst; 249 struct sockaddr_in dst4; 250 } u; 251 struct sockaddr *rdst = &u.dst; /* real IP destination, as opposed 252 * to the nexthop 253 */ 254 struct psref psref, psref_ia; 255 int bound; 256 bool bind_need_restore = 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 267 if (opt) { 268 m = ip_insertoptions(m, opt, &len); 269 if (len >= sizeof(struct ip)) 270 hlen = len; 271 } 272 ip = mtod(m, struct ip *); 273 274 /* 275 * Fill in IP header. 276 */ 277 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 278 ip->ip_v = IPVERSION; 279 ip->ip_off = htons(0); 280 /* ip->ip_id filled in after we find out source ia */ 281 ip->ip_hl = hlen >> 2; 282 IP_STATINC(IP_STAT_LOCALOUT); 283 } else { 284 hlen = ip->ip_hl << 2; 285 } 286 287 /* 288 * Route packet. 289 */ 290 if (ro == NULL) { 291 memset(&iproute, 0, sizeof(iproute)); 292 ro = &iproute; 293 } 294 sockaddr_in_init(&u.dst4, &ip->ip_dst, 0); 295 dst = satocsin(rtcache_getdst(ro)); 296 297 /* 298 * If there is a cached route, check that it is to the same 299 * destination and is still up. If not, free it and try again. 300 * The address family should also be checked in case of sharing 301 * the cache with IPv6. 302 */ 303 if (dst && (dst->sin_family != AF_INET || 304 !in_hosteq(dst->sin_addr, ip->ip_dst))) 305 rtcache_free(ro); 306 307 if ((rt = rtcache_validate(ro)) == NULL && 308 (rt = rtcache_update(ro, 1)) == NULL) { 309 dst = &u.dst4; 310 error = rtcache_setdst(ro, &u.dst); 311 if (error != 0) 312 goto bad; 313 } 314 315 bound = curlwp_bind(); 316 bind_need_restore = true; 317 /* 318 * If routing to interface only, short circuit routing lookup. 319 */ 320 if (flags & IP_ROUTETOIF) { 321 struct ifaddr *ifa; 322 323 ifa = ifa_ifwithladdr_psref(sintocsa(dst), &psref_ia); 324 if (ifa == NULL) { 325 IP_STATINC(IP_STAT_NOROUTE); 326 error = ENETUNREACH; 327 goto bad; 328 } 329 /* ia is already referenced by psref_ia */ 330 ia = ifatoia(ifa); 331 332 ifp = ia->ia_ifp; 333 mtu = ifp->if_mtu; 334 ip->ip_ttl = 1; 335 isbroadcast = in_broadcast(dst->sin_addr, ifp); 336 } else if ((IN_MULTICAST(ip->ip_dst.s_addr) || 337 ip->ip_dst.s_addr == INADDR_BROADCAST) && 338 imo != NULL && imo->imo_multicast_if_index != 0) { 339 ifp = mifp = if_get_byindex(imo->imo_multicast_if_index, &psref); 340 if (ifp == NULL) { 341 IP_STATINC(IP_STAT_NOROUTE); 342 error = ENETUNREACH; 343 goto bad; 344 } 345 mtu = ifp->if_mtu; 346 ia = in_get_ia_from_ifp_psref(ifp, &psref_ia); 347 if (ia == NULL) { 348 error = EADDRNOTAVAIL; 349 goto bad; 350 } 351 isbroadcast = 0; 352 } else { 353 if (rt == NULL) 354 rt = rtcache_init(ro); 355 if (rt == NULL) { 356 IP_STATINC(IP_STAT_NOROUTE); 357 error = EHOSTUNREACH; 358 goto bad; 359 } 360 if (ifa_is_destroying(rt->rt_ifa)) { 361 rtcache_unref(rt, ro); 362 rt = NULL; 363 IP_STATINC(IP_STAT_NOROUTE); 364 error = EHOSTUNREACH; 365 goto bad; 366 } 367 ifa_acquire(rt->rt_ifa, &psref_ia); 368 ia = ifatoia(rt->rt_ifa); 369 ifp = rt->rt_ifp; 370 if ((mtu = rt->rt_rmx.rmx_mtu) == 0) 371 mtu = ifp->if_mtu; 372 rt->rt_use++; 373 if (rt->rt_flags & RTF_GATEWAY) 374 dst = satosin(rt->rt_gateway); 375 if (rt->rt_flags & RTF_HOST) 376 isbroadcast = rt->rt_flags & RTF_BROADCAST; 377 else 378 isbroadcast = in_broadcast(dst->sin_addr, ifp); 379 } 380 rtmtu_nolock = rt && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0; 381 382 if (IN_MULTICAST(ip->ip_dst.s_addr) || 383 (ip->ip_dst.s_addr == INADDR_BROADCAST)) { 384 bool inmgroup; 385 386 m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ? 387 M_BCAST : M_MCAST; 388 /* 389 * See if the caller provided any multicast options 390 */ 391 if (imo != NULL) 392 ip->ip_ttl = imo->imo_multicast_ttl; 393 else 394 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 395 396 /* 397 * if we don't know the outgoing ifp yet, we can't generate 398 * output 399 */ 400 if (!ifp) { 401 IP_STATINC(IP_STAT_NOROUTE); 402 error = ENETUNREACH; 403 goto bad; 404 } 405 406 /* 407 * If the packet is multicast or broadcast, confirm that 408 * the outgoing interface can transmit it. 409 */ 410 if (((m->m_flags & M_MCAST) && 411 (ifp->if_flags & IFF_MULTICAST) == 0) || 412 ((m->m_flags & M_BCAST) && 413 (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)) { 414 IP_STATINC(IP_STAT_NOROUTE); 415 error = ENETUNREACH; 416 goto bad; 417 } 418 /* 419 * If source address not specified yet, use an address 420 * of outgoing interface. 421 */ 422 if (in_nullhost(ip->ip_src)) { 423 struct in_ifaddr *xia; 424 struct ifaddr *xifa; 425 struct psref _psref; 426 427 xia = in_get_ia_from_ifp_psref(ifp, &_psref); 428 if (!xia) { 429 error = EADDRNOTAVAIL; 430 goto bad; 431 } 432 xifa = &xia->ia_ifa; 433 if (xifa->ifa_getifa != NULL) { 434 ia4_release(xia, &_psref); 435 /* FIXME NOMPSAFE */ 436 xia = ifatoia((*xifa->ifa_getifa)(xifa, rdst)); 437 if (xia == NULL) { 438 error = EADDRNOTAVAIL; 439 goto bad; 440 } 441 ia4_acquire(xia, &_psref); 442 } 443 ip->ip_src = xia->ia_addr.sin_addr; 444 ia4_release(xia, &_psref); 445 } 446 447 inmgroup = in_multi_group(ip->ip_dst, ifp, flags); 448 if (inmgroup && (imo == NULL || imo->imo_multicast_loop)) { 449 /* 450 * If we belong to the destination multicast group 451 * on the outgoing interface, and the caller did not 452 * forbid loopback, loop back a copy. 453 */ 454 ip_mloopback(ifp, m, &u.dst4); 455 } 456 #ifdef MROUTING 457 else { 458 /* 459 * If we are acting as a multicast router, perform 460 * multicast forwarding as if the packet had just 461 * arrived on the interface to which we are about 462 * to send. The multicast forwarding function 463 * recursively calls this function, using the 464 * IP_FORWARDING flag to prevent infinite recursion. 465 * 466 * Multicasts that are looped back by ip_mloopback(), 467 * above, will be forwarded by the ip_input() routine, 468 * if necessary. 469 */ 470 extern struct socket *ip_mrouter; 471 472 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 473 if (ip_mforward(m, ifp) != 0) { 474 m_freem(m); 475 goto done; 476 } 477 } 478 } 479 #endif 480 /* 481 * Multicasts with a time-to-live of zero may be looped- 482 * back, above, but must not be transmitted on a network. 483 * Also, multicasts addressed to the loopback interface 484 * are not sent -- the above call to ip_mloopback() will 485 * loop back a copy if this host actually belongs to the 486 * destination group on the loopback interface. 487 */ 488 if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) { 489 m_freem(m); 490 goto done; 491 } 492 goto sendit; 493 } 494 495 /* 496 * If source address not specified yet, use address 497 * of outgoing interface. 498 */ 499 if (in_nullhost(ip->ip_src)) { 500 struct ifaddr *xifa; 501 502 xifa = &ia->ia_ifa; 503 if (xifa->ifa_getifa != NULL) { 504 ia4_release(ia, &psref_ia); 505 /* FIXME NOMPSAFE */ 506 ia = ifatoia((*xifa->ifa_getifa)(xifa, rdst)); 507 if (ia == NULL) { 508 error = EADDRNOTAVAIL; 509 goto bad; 510 } 511 ia4_acquire(ia, &psref_ia); 512 } 513 ip->ip_src = ia->ia_addr.sin_addr; 514 } 515 516 /* 517 * packets with Class-D address as source are not valid per 518 * RFC 1112 519 */ 520 if (IN_MULTICAST(ip->ip_src.s_addr)) { 521 IP_STATINC(IP_STAT_ODROPPED); 522 error = EADDRNOTAVAIL; 523 goto bad; 524 } 525 526 /* 527 * Look for broadcast address and and verify user is allowed to 528 * send such a packet. 529 */ 530 if (isbroadcast) { 531 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 532 error = EADDRNOTAVAIL; 533 goto bad; 534 } 535 if ((flags & IP_ALLOWBROADCAST) == 0) { 536 error = EACCES; 537 goto bad; 538 } 539 /* don't allow broadcast messages to be fragmented */ 540 if (ntohs(ip->ip_len) > ifp->if_mtu) { 541 error = EMSGSIZE; 542 goto bad; 543 } 544 m->m_flags |= M_BCAST; 545 } else 546 m->m_flags &= ~M_BCAST; 547 548 sendit: 549 if ((flags & (IP_FORWARDING|IP_NOIPNEWID)) == 0) { 550 if (m->m_pkthdr.len < IP_MINFRAGSIZE) { 551 ip->ip_id = 0; 552 } else if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) { 553 ip->ip_id = ip_newid(ia); 554 } else { 555 556 /* 557 * TSO capable interfaces (typically?) increment 558 * ip_id for each segment. 559 * "allocate" enough ids here to increase the chance 560 * for them to be unique. 561 * 562 * note that the following calculation is not 563 * needed to be precise. wasting some ip_id is fine. 564 */ 565 566 unsigned int segsz = m->m_pkthdr.segsz; 567 unsigned int datasz = ntohs(ip->ip_len) - hlen; 568 unsigned int num = howmany(datasz, segsz); 569 570 ip->ip_id = ip_newid_range(ia, num); 571 } 572 } 573 if (ia != NULL) { 574 ia4_release(ia, &psref_ia); 575 ia = NULL; 576 } 577 578 /* 579 * If we're doing Path MTU Discovery, we need to set DF unless 580 * the route's MTU is locked. 581 */ 582 if ((flags & IP_MTUDISC) != 0 && rtmtu_nolock) { 583 ip->ip_off |= htons(IP_DF); 584 } 585 586 #ifdef IPSEC 587 if (ipsec_used) { 588 bool ipsec_done = false; 589 590 /* Perform IPsec processing, if any. */ 591 error = ipsec4_output(m, so, flags, &sp, &mtu, &natt_frag, 592 &ipsec_done); 593 if (error || ipsec_done) 594 goto done; 595 } 596 #endif 597 598 /* 599 * Run through list of hooks for output packets. 600 */ 601 error = pfil_run_hooks(inet_pfil_hook, &m, ifp, PFIL_OUT); 602 if (error) 603 goto done; 604 if (m == NULL) 605 goto done; 606 607 ip = mtod(m, struct ip *); 608 hlen = ip->ip_hl << 2; 609 610 m->m_pkthdr.csum_data |= hlen << 16; 611 612 /* 613 * search for the source address structure to 614 * maintain output statistics. 615 */ 616 KASSERT(ia == NULL); 617 ia = in_get_ia_psref(ip->ip_src, &psref_ia); 618 619 /* Ensure we only send from a valid address. */ 620 if ((ia != NULL || (flags & IP_FORWARDING) == 0) && 621 (error = ip_ifaddrvalid(ia)) != 0) 622 { 623 ARPLOG(LOG_ERR, 624 "refusing to send from invalid address %s (pid %d)\n", 625 ARPLOGADDR(ip->ip_src), curproc->p_pid); 626 IP_STATINC(IP_STAT_ODROPPED); 627 if (error == 1) 628 /* 629 * Address exists, but is tentative or detached. 630 * We can't send from it because it's invalid, 631 * so we drop the packet. 632 */ 633 error = 0; 634 else 635 error = EADDRNOTAVAIL; 636 goto bad; 637 } 638 639 /* Maybe skip checksums on loopback interfaces. */ 640 if (IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) { 641 m->m_pkthdr.csum_flags |= M_CSUM_IPv4; 642 } 643 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx; 644 /* 645 * If small enough for mtu of path, or if using TCP segmentation 646 * offload, can just send directly. 647 */ 648 if (ntohs(ip->ip_len) <= mtu || 649 (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0) { 650 const struct sockaddr *sa; 651 652 #if IFA_STATS 653 if (ia) 654 ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len); 655 #endif 656 /* 657 * Always initialize the sum to 0! Some HW assisted 658 * checksumming requires this. 659 */ 660 ip->ip_sum = 0; 661 662 if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) { 663 /* 664 * Perform any checksums that the hardware can't do 665 * for us. 666 * 667 * XXX Does any hardware require the {th,uh}_sum 668 * XXX fields to be 0? 669 */ 670 if (sw_csum & M_CSUM_IPv4) { 671 KASSERT(IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)); 672 ip->ip_sum = in_cksum(m, hlen); 673 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4; 674 } 675 if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 676 if (IN_NEED_CHECKSUM(ifp, 677 sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4))) { 678 in_delayed_cksum(m); 679 } 680 m->m_pkthdr.csum_flags &= 681 ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 682 } 683 } 684 685 sa = (m->m_flags & M_MCAST) ? sintocsa(rdst) : sintocsa(dst); 686 if (__predict_true( 687 (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0 || 688 (ifp->if_capenable & IFCAP_TSOv4) != 0)) { 689 error = ip_if_output(ifp, m, sa, rt); 690 } else { 691 error = ip_tso_output(ifp, m, sa, rt); 692 } 693 goto done; 694 } 695 696 /* 697 * We can't use HW checksumming if we're about to 698 * to fragment the packet. 699 * 700 * XXX Some hardware can do this. 701 */ 702 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 703 if (IN_NEED_CHECKSUM(ifp, 704 m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))) { 705 in_delayed_cksum(m); 706 } 707 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 708 } 709 710 /* 711 * Too large for interface; fragment if possible. 712 * Must be able to put at least 8 bytes per fragment. 713 */ 714 if (ntohs(ip->ip_off) & IP_DF) { 715 if (flags & IP_RETURNMTU) { 716 struct inpcb *inp; 717 718 KASSERT(so && solocked(so)); 719 inp = sotoinpcb(so); 720 inp->inp_errormtu = mtu; 721 } 722 error = EMSGSIZE; 723 IP_STATINC(IP_STAT_CANTFRAG); 724 goto bad; 725 } 726 727 error = ip_fragment(m, ifp, mtu); 728 if (error) { 729 m = NULL; 730 goto bad; 731 } 732 733 for (; m; m = m0) { 734 m0 = m->m_nextpkt; 735 m->m_nextpkt = 0; 736 if (error) { 737 m_freem(m); 738 continue; 739 } 740 #if IFA_STATS 741 if (ia) 742 ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len); 743 #endif 744 /* 745 * If we get there, the packet has not been handled by 746 * IPsec whereas it should have. Now that it has been 747 * fragmented, re-inject it in ip_output so that IPsec 748 * processing can occur. 749 */ 750 if (natt_frag) { 751 error = ip_output(m, opt, ro, 752 flags | IP_RAWOUTPUT | IP_NOIPNEWID, 753 imo, so); 754 } else { 755 KASSERT((m->m_pkthdr.csum_flags & 756 (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0); 757 error = ip_if_output(ifp, m, 758 (m->m_flags & M_MCAST) ? 759 sintocsa(rdst) : sintocsa(dst), rt); 760 } 761 } 762 if (error == 0) { 763 IP_STATINC(IP_STAT_FRAGMENTED); 764 } 765 done: 766 ia4_release(ia, &psref_ia); 767 rtcache_unref(rt, ro); 768 if (ro == &iproute) { 769 rtcache_free(&iproute); 770 } 771 #ifdef IPSEC 772 if (sp) { 773 KEY_FREESP(&sp); 774 } 775 #endif 776 if (mifp != NULL) { 777 if_put(mifp, &psref); 778 } 779 if (bind_need_restore) 780 curlwp_bindx(bound); 781 return error; 782 bad: 783 m_freem(m); 784 goto done; 785 } 786 787 int 788 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu) 789 { 790 struct ip *ip, *mhip; 791 struct mbuf *m0; 792 int len, hlen, off; 793 int mhlen, firstlen; 794 struct mbuf **mnext; 795 int sw_csum = m->m_pkthdr.csum_flags; 796 int fragments = 0; 797 int error = 0; 798 799 ip = mtod(m, struct ip *); 800 hlen = ip->ip_hl << 2; 801 if (ifp != NULL) 802 sw_csum &= ~ifp->if_csum_flags_tx; 803 804 len = (mtu - hlen) &~ 7; 805 if (len < 8) { 806 m_freem(m); 807 return (EMSGSIZE); 808 } 809 810 firstlen = len; 811 mnext = &m->m_nextpkt; 812 813 /* 814 * Loop through length of segment after first fragment, 815 * make new header and copy data of each part and link onto chain. 816 */ 817 m0 = m; 818 mhlen = sizeof (struct ip); 819 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) { 820 MGETHDR(m, M_DONTWAIT, MT_HEADER); 821 if (m == 0) { 822 error = ENOBUFS; 823 IP_STATINC(IP_STAT_ODROPPED); 824 goto sendorfree; 825 } 826 MCLAIM(m, m0->m_owner); 827 *mnext = m; 828 mnext = &m->m_nextpkt; 829 m->m_data += max_linkhdr; 830 mhip = mtod(m, struct ip *); 831 *mhip = *ip; 832 /* we must inherit MCAST and BCAST flags */ 833 m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST); 834 if (hlen > sizeof (struct ip)) { 835 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 836 mhip->ip_hl = mhlen >> 2; 837 } 838 m->m_len = mhlen; 839 mhip->ip_off = ((off - hlen) >> 3) + 840 (ntohs(ip->ip_off) & ~IP_MF); 841 if (ip->ip_off & htons(IP_MF)) 842 mhip->ip_off |= IP_MF; 843 if (off + len >= ntohs(ip->ip_len)) 844 len = ntohs(ip->ip_len) - off; 845 else 846 mhip->ip_off |= IP_MF; 847 HTONS(mhip->ip_off); 848 mhip->ip_len = htons((u_int16_t)(len + mhlen)); 849 m->m_next = m_copym(m0, off, len, M_DONTWAIT); 850 if (m->m_next == 0) { 851 error = ENOBUFS; /* ??? */ 852 IP_STATINC(IP_STAT_ODROPPED); 853 goto sendorfree; 854 } 855 m->m_pkthdr.len = mhlen + len; 856 m_reset_rcvif(m); 857 mhip->ip_sum = 0; 858 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0); 859 if (sw_csum & M_CSUM_IPv4) { 860 mhip->ip_sum = in_cksum(m, mhlen); 861 } else { 862 /* 863 * checksum is hw-offloaded or not necessary. 864 */ 865 m->m_pkthdr.csum_flags |= 866 m0->m_pkthdr.csum_flags & M_CSUM_IPv4; 867 m->m_pkthdr.csum_data |= mhlen << 16; 868 KASSERT(!(ifp != NULL && 869 IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) || 870 (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0); 871 } 872 IP_STATINC(IP_STAT_OFRAGMENTS); 873 fragments++; 874 } 875 /* 876 * Update first fragment by trimming what's been copied out 877 * and updating header, then send each fragment (in order). 878 */ 879 m = m0; 880 m_adj(m, hlen + firstlen - ntohs(ip->ip_len)); 881 m->m_pkthdr.len = hlen + firstlen; 882 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len); 883 ip->ip_off |= htons(IP_MF); 884 ip->ip_sum = 0; 885 if (sw_csum & M_CSUM_IPv4) { 886 ip->ip_sum = in_cksum(m, hlen); 887 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4; 888 } else { 889 /* 890 * checksum is hw-offloaded or not necessary. 891 */ 892 KASSERT(!(ifp != NULL && IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) || 893 (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0); 894 KASSERT(M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data) >= 895 sizeof(struct ip)); 896 } 897 sendorfree: 898 /* 899 * If there is no room for all the fragments, don't queue 900 * any of them. 901 */ 902 if (ifp != NULL) { 903 IFQ_LOCK(&ifp->if_snd); 904 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments && 905 error == 0) { 906 error = ENOBUFS; 907 IP_STATINC(IP_STAT_ODROPPED); 908 IFQ_INC_DROPS(&ifp->if_snd); 909 } 910 IFQ_UNLOCK(&ifp->if_snd); 911 } 912 if (error) { 913 for (m = m0; m; m = m0) { 914 m0 = m->m_nextpkt; 915 m->m_nextpkt = NULL; 916 m_freem(m); 917 } 918 } 919 return (error); 920 } 921 922 /* 923 * Process a delayed payload checksum calculation. 924 */ 925 void 926 in_delayed_cksum(struct mbuf *m) 927 { 928 struct ip *ip; 929 u_int16_t csum, offset; 930 931 ip = mtod(m, struct ip *); 932 offset = ip->ip_hl << 2; 933 csum = in4_cksum(m, 0, offset, ntohs(ip->ip_len) - offset); 934 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv4) != 0) 935 csum = 0xffff; 936 937 offset += M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data); 938 939 if ((offset + sizeof(u_int16_t)) > m->m_len) { 940 /* This happen when ip options were inserted 941 printf("in_delayed_cksum: pullup len %d off %d proto %d\n", 942 m->m_len, offset, ip->ip_p); 943 */ 944 m_copyback(m, offset, sizeof(csum), (void *) &csum); 945 } else 946 *(u_int16_t *)(mtod(m, char *) + offset) = csum; 947 } 948 949 /* 950 * Determine the maximum length of the options to be inserted; 951 * we would far rather allocate too much space rather than too little. 952 */ 953 954 u_int 955 ip_optlen(struct inpcb *inp) 956 { 957 struct mbuf *m = inp->inp_options; 958 959 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst)) { 960 return (m->m_len - offsetof(struct ipoption, ipopt_dst)); 961 } 962 return 0; 963 } 964 965 /* 966 * Insert IP options into preformed packet. 967 * Adjust IP destination as required for IP source routing, 968 * as indicated by a non-zero in_addr at the start of the options. 969 */ 970 static struct mbuf * 971 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen) 972 { 973 struct ipoption *p = mtod(opt, struct ipoption *); 974 struct mbuf *n; 975 struct ip *ip = mtod(m, struct ip *); 976 unsigned optlen; 977 978 optlen = opt->m_len - sizeof(p->ipopt_dst); 979 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) 980 return (m); /* XXX should fail */ 981 if (!in_nullhost(p->ipopt_dst)) 982 ip->ip_dst = p->ipopt_dst; 983 if (M_READONLY(m) || M_LEADINGSPACE(m) < optlen) { 984 MGETHDR(n, M_DONTWAIT, MT_HEADER); 985 if (n == 0) 986 return (m); 987 MCLAIM(n, m->m_owner); 988 M_MOVE_PKTHDR(n, m); 989 m->m_len -= sizeof(struct ip); 990 m->m_data += sizeof(struct ip); 991 n->m_next = m; 992 m = n; 993 m->m_len = optlen + sizeof(struct ip); 994 m->m_data += max_linkhdr; 995 bcopy((void *)ip, mtod(m, void *), sizeof(struct ip)); 996 } else { 997 m->m_data -= optlen; 998 m->m_len += optlen; 999 memmove(mtod(m, void *), ip, sizeof(struct ip)); 1000 } 1001 m->m_pkthdr.len += optlen; 1002 ip = mtod(m, struct ip *); 1003 bcopy((void *)p->ipopt_list, (void *)(ip + 1), (unsigned)optlen); 1004 *phlen = sizeof(struct ip) + optlen; 1005 ip->ip_len = htons(ntohs(ip->ip_len) + optlen); 1006 return (m); 1007 } 1008 1009 /* 1010 * Copy options from ip to jp, 1011 * omitting those not copied during fragmentation. 1012 */ 1013 int 1014 ip_optcopy(struct ip *ip, struct ip *jp) 1015 { 1016 u_char *cp, *dp; 1017 int opt, optlen, cnt; 1018 1019 cp = (u_char *)(ip + 1); 1020 dp = (u_char *)(jp + 1); 1021 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1022 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1023 opt = cp[0]; 1024 if (opt == IPOPT_EOL) 1025 break; 1026 if (opt == IPOPT_NOP) { 1027 /* Preserve for IP mcast tunnel's LSRR alignment. */ 1028 *dp++ = IPOPT_NOP; 1029 optlen = 1; 1030 continue; 1031 } 1032 1033 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp)); 1034 optlen = cp[IPOPT_OLEN]; 1035 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen < cnt); 1036 1037 /* Invalid lengths should have been caught by ip_dooptions. */ 1038 if (optlen > cnt) 1039 optlen = cnt; 1040 if (IPOPT_COPIED(opt)) { 1041 bcopy((void *)cp, (void *)dp, (unsigned)optlen); 1042 dp += optlen; 1043 } 1044 } 1045 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1046 *dp++ = IPOPT_EOL; 1047 return (optlen); 1048 } 1049 1050 /* 1051 * IP socket option processing. 1052 */ 1053 int 1054 ip_ctloutput(int op, struct socket *so, struct sockopt *sopt) 1055 { 1056 struct inpcb *inp = sotoinpcb(so); 1057 struct ip *ip = &inp->inp_ip; 1058 int inpflags = inp->inp_flags; 1059 int optval = 0, error = 0; 1060 1061 if (sopt->sopt_level != IPPROTO_IP) { 1062 if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) 1063 return 0; 1064 return ENOPROTOOPT; 1065 } 1066 1067 switch (op) { 1068 case PRCO_SETOPT: 1069 switch (sopt->sopt_name) { 1070 case IP_OPTIONS: 1071 #ifdef notyet 1072 case IP_RETOPTS: 1073 #endif 1074 error = ip_pcbopts(inp, sopt); 1075 break; 1076 1077 case IP_TOS: 1078 case IP_TTL: 1079 case IP_MINTTL: 1080 case IP_PKTINFO: 1081 case IP_RECVOPTS: 1082 case IP_RECVRETOPTS: 1083 case IP_RECVDSTADDR: 1084 case IP_RECVIF: 1085 case IP_RECVPKTINFO: 1086 case IP_RECVTTL: 1087 error = sockopt_getint(sopt, &optval); 1088 if (error) 1089 break; 1090 1091 switch (sopt->sopt_name) { 1092 case IP_TOS: 1093 ip->ip_tos = optval; 1094 break; 1095 1096 case IP_TTL: 1097 ip->ip_ttl = optval; 1098 break; 1099 1100 case IP_MINTTL: 1101 if (optval > 0 && optval <= MAXTTL) 1102 inp->inp_ip_minttl = optval; 1103 else 1104 error = EINVAL; 1105 break; 1106 #define OPTSET(bit) \ 1107 if (optval) \ 1108 inpflags |= bit; \ 1109 else \ 1110 inpflags &= ~bit; 1111 1112 case IP_PKTINFO: 1113 OPTSET(INP_PKTINFO); 1114 break; 1115 1116 case IP_RECVOPTS: 1117 OPTSET(INP_RECVOPTS); 1118 break; 1119 1120 case IP_RECVPKTINFO: 1121 OPTSET(INP_RECVPKTINFO); 1122 break; 1123 1124 case IP_RECVRETOPTS: 1125 OPTSET(INP_RECVRETOPTS); 1126 break; 1127 1128 case IP_RECVDSTADDR: 1129 OPTSET(INP_RECVDSTADDR); 1130 break; 1131 1132 case IP_RECVIF: 1133 OPTSET(INP_RECVIF); 1134 break; 1135 1136 case IP_RECVTTL: 1137 OPTSET(INP_RECVTTL); 1138 break; 1139 } 1140 break; 1141 #undef OPTSET 1142 1143 case IP_MULTICAST_IF: 1144 case IP_MULTICAST_TTL: 1145 case IP_MULTICAST_LOOP: 1146 case IP_ADD_MEMBERSHIP: 1147 case IP_DROP_MEMBERSHIP: 1148 error = ip_setmoptions(&inp->inp_moptions, sopt); 1149 break; 1150 1151 case IP_PORTRANGE: 1152 error = sockopt_getint(sopt, &optval); 1153 if (error) 1154 break; 1155 1156 switch (optval) { 1157 case IP_PORTRANGE_DEFAULT: 1158 case IP_PORTRANGE_HIGH: 1159 inpflags &= ~(INP_LOWPORT); 1160 break; 1161 1162 case IP_PORTRANGE_LOW: 1163 inpflags |= INP_LOWPORT; 1164 break; 1165 1166 default: 1167 error = EINVAL; 1168 break; 1169 } 1170 break; 1171 1172 case IP_PORTALGO: 1173 error = sockopt_getint(sopt, &optval); 1174 if (error) 1175 break; 1176 1177 error = portalgo_algo_index_select( 1178 (struct inpcb_hdr *)inp, optval); 1179 break; 1180 1181 #if defined(IPSEC) 1182 case IP_IPSEC_POLICY: 1183 if (ipsec_enabled) { 1184 error = ipsec4_set_policy(inp, sopt->sopt_name, 1185 sopt->sopt_data, sopt->sopt_size, 1186 curlwp->l_cred); 1187 break; 1188 } 1189 /*FALLTHROUGH*/ 1190 #endif /* IPSEC */ 1191 1192 default: 1193 error = ENOPROTOOPT; 1194 break; 1195 } 1196 break; 1197 1198 case PRCO_GETOPT: 1199 switch (sopt->sopt_name) { 1200 case IP_OPTIONS: 1201 case IP_RETOPTS: { 1202 struct mbuf *mopts = inp->inp_options; 1203 1204 if (mopts) { 1205 struct mbuf *m; 1206 1207 m = m_copym(mopts, 0, M_COPYALL, M_DONTWAIT); 1208 if (m == NULL) { 1209 error = ENOBUFS; 1210 break; 1211 } 1212 error = sockopt_setmbuf(sopt, m); 1213 } 1214 break; 1215 } 1216 case IP_PKTINFO: 1217 case IP_TOS: 1218 case IP_TTL: 1219 case IP_MINTTL: 1220 case IP_RECVOPTS: 1221 case IP_RECVRETOPTS: 1222 case IP_RECVDSTADDR: 1223 case IP_RECVIF: 1224 case IP_RECVPKTINFO: 1225 case IP_RECVTTL: 1226 case IP_ERRORMTU: 1227 switch (sopt->sopt_name) { 1228 case IP_TOS: 1229 optval = ip->ip_tos; 1230 break; 1231 1232 case IP_TTL: 1233 optval = ip->ip_ttl; 1234 break; 1235 1236 case IP_MINTTL: 1237 optval = inp->inp_ip_minttl; 1238 break; 1239 1240 case IP_ERRORMTU: 1241 optval = inp->inp_errormtu; 1242 break; 1243 1244 #define OPTBIT(bit) (inpflags & bit ? 1 : 0) 1245 1246 case IP_PKTINFO: 1247 optval = OPTBIT(INP_PKTINFO); 1248 break; 1249 1250 case IP_RECVOPTS: 1251 optval = OPTBIT(INP_RECVOPTS); 1252 break; 1253 1254 case IP_RECVPKTINFO: 1255 optval = OPTBIT(INP_RECVPKTINFO); 1256 break; 1257 1258 case IP_RECVRETOPTS: 1259 optval = OPTBIT(INP_RECVRETOPTS); 1260 break; 1261 1262 case IP_RECVDSTADDR: 1263 optval = OPTBIT(INP_RECVDSTADDR); 1264 break; 1265 1266 case IP_RECVIF: 1267 optval = OPTBIT(INP_RECVIF); 1268 break; 1269 1270 case IP_RECVTTL: 1271 optval = OPTBIT(INP_RECVTTL); 1272 break; 1273 } 1274 error = sockopt_setint(sopt, optval); 1275 break; 1276 1277 #if 0 /* defined(IPSEC) */ 1278 case IP_IPSEC_POLICY: 1279 { 1280 struct mbuf *m = NULL; 1281 1282 /* XXX this will return EINVAL as sopt is empty */ 1283 error = ipsec4_get_policy(inp, sopt->sopt_data, 1284 sopt->sopt_size, &m); 1285 if (error == 0) 1286 error = sockopt_setmbuf(sopt, m); 1287 break; 1288 } 1289 #endif /*IPSEC*/ 1290 1291 case IP_MULTICAST_IF: 1292 case IP_MULTICAST_TTL: 1293 case IP_MULTICAST_LOOP: 1294 case IP_ADD_MEMBERSHIP: 1295 case IP_DROP_MEMBERSHIP: 1296 error = ip_getmoptions(inp->inp_moptions, sopt); 1297 break; 1298 1299 case IP_PORTRANGE: 1300 if (inpflags & INP_LOWPORT) 1301 optval = IP_PORTRANGE_LOW; 1302 else 1303 optval = IP_PORTRANGE_DEFAULT; 1304 error = sockopt_setint(sopt, optval); 1305 break; 1306 1307 case IP_PORTALGO: 1308 optval = inp->inp_portalgo; 1309 error = sockopt_setint(sopt, optval); 1310 break; 1311 1312 default: 1313 error = ENOPROTOOPT; 1314 break; 1315 } 1316 break; 1317 } 1318 1319 if (!error) { 1320 inp->inp_flags = inpflags; 1321 } 1322 return error; 1323 } 1324 1325 /* 1326 * Set up IP options in pcb for insertion in output packets. 1327 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1328 * with destination address if source routed. 1329 */ 1330 static int 1331 ip_pcbopts(struct inpcb *inp, const struct sockopt *sopt) 1332 { 1333 struct mbuf *m; 1334 const u_char *cp; 1335 u_char *dp; 1336 int cnt; 1337 1338 /* Turn off any old options. */ 1339 if (inp->inp_options) { 1340 m_free(inp->inp_options); 1341 } 1342 inp->inp_options = NULL; 1343 if ((cnt = sopt->sopt_size) == 0) { 1344 /* Only turning off any previous options. */ 1345 return 0; 1346 } 1347 cp = sopt->sopt_data; 1348 1349 #ifndef __vax__ 1350 if (cnt % sizeof(int32_t)) 1351 return (EINVAL); 1352 #endif 1353 1354 m = m_get(M_DONTWAIT, MT_SOOPTS); 1355 if (m == NULL) 1356 return (ENOBUFS); 1357 1358 dp = mtod(m, u_char *); 1359 memset(dp, 0, sizeof(struct in_addr)); 1360 dp += sizeof(struct in_addr); 1361 m->m_len = sizeof(struct in_addr); 1362 1363 /* 1364 * IP option list according to RFC791. Each option is of the form 1365 * 1366 * [optval] [olen] [(olen - 2) data bytes] 1367 * 1368 * We validate the list and copy options to an mbuf for prepending 1369 * to data packets. The IP first-hop destination address will be 1370 * stored before actual options and is zero if unset. 1371 */ 1372 while (cnt > 0) { 1373 uint8_t optval, olen, offset; 1374 1375 optval = cp[IPOPT_OPTVAL]; 1376 1377 if (optval == IPOPT_EOL || optval == IPOPT_NOP) { 1378 olen = 1; 1379 } else { 1380 if (cnt < IPOPT_OLEN + 1) 1381 goto bad; 1382 1383 olen = cp[IPOPT_OLEN]; 1384 if (olen < IPOPT_OLEN + 1 || olen > cnt) 1385 goto bad; 1386 } 1387 1388 if (optval == IPOPT_LSRR || optval == IPOPT_SSRR) { 1389 /* 1390 * user process specifies route as: 1391 * ->A->B->C->D 1392 * D must be our final destination (but we can't 1393 * check that since we may not have connected yet). 1394 * A is first hop destination, which doesn't appear in 1395 * actual IP option, but is stored before the options. 1396 */ 1397 if (olen < IPOPT_OFFSET + 1 + sizeof(struct in_addr)) 1398 goto bad; 1399 1400 offset = cp[IPOPT_OFFSET]; 1401 memcpy(mtod(m, u_char *), cp + IPOPT_OFFSET + 1, 1402 sizeof(struct in_addr)); 1403 1404 cp += sizeof(struct in_addr); 1405 cnt -= sizeof(struct in_addr); 1406 olen -= sizeof(struct in_addr); 1407 1408 if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr)) 1409 goto bad; 1410 1411 memcpy(dp, cp, olen); 1412 dp[IPOPT_OPTVAL] = optval; 1413 dp[IPOPT_OLEN] = olen; 1414 dp[IPOPT_OFFSET] = offset; 1415 break; 1416 } else { 1417 if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr)) 1418 goto bad; 1419 1420 memcpy(dp, cp, olen); 1421 break; 1422 } 1423 1424 dp += olen; 1425 m->m_len += olen; 1426 1427 if (optval == IPOPT_EOL) 1428 break; 1429 1430 cp += olen; 1431 cnt -= olen; 1432 } 1433 1434 inp->inp_options = m; 1435 return 0; 1436 bad: 1437 (void)m_free(m); 1438 return EINVAL; 1439 } 1440 1441 /* 1442 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1443 */ 1444 static struct ifnet * 1445 ip_multicast_if(struct in_addr *a, int *ifindexp) 1446 { 1447 int ifindex; 1448 struct ifnet *ifp = NULL; 1449 struct in_ifaddr *ia; 1450 1451 if (ifindexp) 1452 *ifindexp = 0; 1453 if (ntohl(a->s_addr) >> 24 == 0) { 1454 ifindex = ntohl(a->s_addr) & 0xffffff; 1455 ifp = if_byindex(ifindex); 1456 if (!ifp) 1457 return NULL; 1458 if (ifindexp) 1459 *ifindexp = ifindex; 1460 } else { 1461 LIST_FOREACH(ia, &IN_IFADDR_HASH(a->s_addr), ia_hash) { 1462 if (in_hosteq(ia->ia_addr.sin_addr, *a) && 1463 (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) { 1464 ifp = ia->ia_ifp; 1465 break; 1466 } 1467 } 1468 } 1469 return ifp; 1470 } 1471 1472 static int 1473 ip_getoptval(const struct sockopt *sopt, u_int8_t *val, u_int maxval) 1474 { 1475 u_int tval; 1476 u_char cval; 1477 int error; 1478 1479 if (sopt == NULL) 1480 return EINVAL; 1481 1482 switch (sopt->sopt_size) { 1483 case sizeof(u_char): 1484 error = sockopt_get(sopt, &cval, sizeof(u_char)); 1485 tval = cval; 1486 break; 1487 1488 case sizeof(u_int): 1489 error = sockopt_get(sopt, &tval, sizeof(u_int)); 1490 break; 1491 1492 default: 1493 error = EINVAL; 1494 } 1495 1496 if (error) 1497 return error; 1498 1499 if (tval > maxval) 1500 return EINVAL; 1501 1502 *val = tval; 1503 return 0; 1504 } 1505 1506 static int 1507 ip_get_membership(const struct sockopt *sopt, struct ifnet **ifp, 1508 struct in_addr *ia, bool add) 1509 { 1510 int error; 1511 struct ip_mreq mreq; 1512 1513 error = sockopt_get(sopt, &mreq, sizeof(mreq)); 1514 if (error) 1515 return error; 1516 1517 if (!IN_MULTICAST(mreq.imr_multiaddr.s_addr)) 1518 return EINVAL; 1519 1520 memcpy(ia, &mreq.imr_multiaddr, sizeof(*ia)); 1521 1522 if (in_nullhost(mreq.imr_interface)) { 1523 union { 1524 struct sockaddr dst; 1525 struct sockaddr_in dst4; 1526 } u; 1527 struct route ro; 1528 1529 if (!add) { 1530 *ifp = NULL; 1531 return 0; 1532 } 1533 /* 1534 * If no interface address was provided, use the interface of 1535 * the route to the given multicast address. 1536 */ 1537 struct rtentry *rt; 1538 memset(&ro, 0, sizeof(ro)); 1539 1540 sockaddr_in_init(&u.dst4, ia, 0); 1541 error = rtcache_setdst(&ro, &u.dst); 1542 if (error != 0) 1543 return error; 1544 *ifp = (rt = rtcache_init(&ro)) != NULL ? rt->rt_ifp : NULL; 1545 rtcache_unref(rt, &ro); 1546 rtcache_free(&ro); 1547 } else { 1548 *ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1549 if (!add && *ifp == NULL) 1550 return EADDRNOTAVAIL; 1551 } 1552 return 0; 1553 } 1554 1555 /* 1556 * Add a multicast group membership. 1557 * Group must be a valid IP multicast address. 1558 */ 1559 static int 1560 ip_add_membership(struct ip_moptions *imo, const struct sockopt *sopt) 1561 { 1562 struct ifnet *ifp = NULL; // XXX: gcc [ppc] 1563 struct in_addr ia; 1564 int i, error; 1565 1566 if (sopt->sopt_size == sizeof(struct ip_mreq)) 1567 error = ip_get_membership(sopt, &ifp, &ia, true); 1568 else 1569 #ifdef INET6 1570 error = ip6_get_membership(sopt, &ifp, &ia, sizeof(ia)); 1571 #else 1572 return EINVAL; 1573 #endif 1574 1575 if (error) 1576 return error; 1577 1578 /* 1579 * See if we found an interface, and confirm that it 1580 * supports multicast. 1581 */ 1582 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) 1583 return EADDRNOTAVAIL; 1584 1585 /* 1586 * See if the membership already exists or if all the 1587 * membership slots are full. 1588 */ 1589 for (i = 0; i < imo->imo_num_memberships; ++i) { 1590 if (imo->imo_membership[i]->inm_ifp == ifp && 1591 in_hosteq(imo->imo_membership[i]->inm_addr, ia)) 1592 break; 1593 } 1594 if (i < imo->imo_num_memberships) 1595 return EADDRINUSE; 1596 1597 if (i == IP_MAX_MEMBERSHIPS) 1598 return ETOOMANYREFS; 1599 1600 /* 1601 * Everything looks good; add a new record to the multicast 1602 * address list for the given interface. 1603 */ 1604 if ((imo->imo_membership[i] = in_addmulti(&ia, ifp)) == NULL) 1605 return ENOBUFS; 1606 1607 ++imo->imo_num_memberships; 1608 return 0; 1609 } 1610 1611 /* 1612 * Drop a multicast group membership. 1613 * Group must be a valid IP multicast address. 1614 */ 1615 static int 1616 ip_drop_membership(struct ip_moptions *imo, const struct sockopt *sopt) 1617 { 1618 struct in_addr ia = { .s_addr = 0 }; // XXX: gcc [ppc] 1619 struct ifnet *ifp = NULL; // XXX: gcc [ppc] 1620 int i, error; 1621 1622 if (sopt->sopt_size == sizeof(struct ip_mreq)) 1623 error = ip_get_membership(sopt, &ifp, &ia, false); 1624 else 1625 #ifdef INET6 1626 error = ip6_get_membership(sopt, &ifp, &ia, sizeof(ia)); 1627 #else 1628 return EINVAL; 1629 #endif 1630 1631 if (error) 1632 return error; 1633 1634 /* 1635 * Find the membership in the membership array. 1636 */ 1637 for (i = 0; i < imo->imo_num_memberships; ++i) { 1638 if ((ifp == NULL || 1639 imo->imo_membership[i]->inm_ifp == ifp) && 1640 in_hosteq(imo->imo_membership[i]->inm_addr, ia)) 1641 break; 1642 } 1643 if (i == imo->imo_num_memberships) 1644 return EADDRNOTAVAIL; 1645 1646 /* 1647 * Give up the multicast address record to which the 1648 * membership points. 1649 */ 1650 in_delmulti(imo->imo_membership[i]); 1651 1652 /* 1653 * Remove the gap in the membership array. 1654 */ 1655 for (++i; i < imo->imo_num_memberships; ++i) 1656 imo->imo_membership[i-1] = imo->imo_membership[i]; 1657 --imo->imo_num_memberships; 1658 return 0; 1659 } 1660 1661 /* 1662 * Set the IP multicast options in response to user setsockopt(). 1663 */ 1664 int 1665 ip_setmoptions(struct ip_moptions **pimo, const struct sockopt *sopt) 1666 { 1667 struct ip_moptions *imo = *pimo; 1668 struct in_addr addr; 1669 struct ifnet *ifp; 1670 int ifindex, error = 0; 1671 1672 if (!imo) { 1673 /* 1674 * No multicast option buffer attached to the pcb; 1675 * allocate one and initialize to default values. 1676 */ 1677 imo = kmem_intr_alloc(sizeof(*imo), KM_NOSLEEP); 1678 if (imo == NULL) 1679 return ENOBUFS; 1680 1681 imo->imo_multicast_if_index = 0; 1682 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1683 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1684 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1685 imo->imo_num_memberships = 0; 1686 *pimo = imo; 1687 } 1688 1689 switch (sopt->sopt_name) { 1690 case IP_MULTICAST_IF: 1691 /* 1692 * Select the interface for outgoing multicast packets. 1693 */ 1694 error = sockopt_get(sopt, &addr, sizeof(addr)); 1695 if (error) 1696 break; 1697 1698 /* 1699 * INADDR_ANY is used to remove a previous selection. 1700 * When no interface is selected, a default one is 1701 * chosen every time a multicast packet is sent. 1702 */ 1703 if (in_nullhost(addr)) { 1704 imo->imo_multicast_if_index = 0; 1705 break; 1706 } 1707 /* 1708 * The selected interface is identified by its local 1709 * IP address. Find the interface and confirm that 1710 * it supports multicasting. 1711 */ 1712 ifp = ip_multicast_if(&addr, &ifindex); 1713 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1714 error = EADDRNOTAVAIL; 1715 break; 1716 } 1717 imo->imo_multicast_if_index = ifp->if_index; 1718 if (ifindex) 1719 imo->imo_multicast_addr = addr; 1720 else 1721 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1722 break; 1723 1724 case IP_MULTICAST_TTL: 1725 /* 1726 * Set the IP time-to-live for outgoing multicast packets. 1727 */ 1728 error = ip_getoptval(sopt, &imo->imo_multicast_ttl, MAXTTL); 1729 break; 1730 1731 case IP_MULTICAST_LOOP: 1732 /* 1733 * Set the loopback flag for outgoing multicast packets. 1734 * Must be zero or one. 1735 */ 1736 error = ip_getoptval(sopt, &imo->imo_multicast_loop, 1); 1737 break; 1738 1739 case IP_ADD_MEMBERSHIP: /* IPV6_JOIN_GROUP */ 1740 error = ip_add_membership(imo, sopt); 1741 break; 1742 1743 case IP_DROP_MEMBERSHIP: /* IPV6_LEAVE_GROUP */ 1744 error = ip_drop_membership(imo, sopt); 1745 break; 1746 1747 default: 1748 error = EOPNOTSUPP; 1749 break; 1750 } 1751 1752 /* 1753 * If all options have default values, no need to keep the mbuf. 1754 */ 1755 if (imo->imo_multicast_if_index == 0 && 1756 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1757 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1758 imo->imo_num_memberships == 0) { 1759 kmem_free(imo, sizeof(*imo)); 1760 *pimo = NULL; 1761 } 1762 1763 return error; 1764 } 1765 1766 /* 1767 * Return the IP multicast options in response to user getsockopt(). 1768 */ 1769 int 1770 ip_getmoptions(struct ip_moptions *imo, struct sockopt *sopt) 1771 { 1772 struct in_addr addr; 1773 uint8_t optval; 1774 int error = 0; 1775 1776 switch (sopt->sopt_name) { 1777 case IP_MULTICAST_IF: 1778 if (imo == NULL || imo->imo_multicast_if_index == 0) 1779 addr = zeroin_addr; 1780 else if (imo->imo_multicast_addr.s_addr) { 1781 /* return the value user has set */ 1782 addr = imo->imo_multicast_addr; 1783 } else { 1784 struct ifnet *ifp; 1785 struct in_ifaddr *ia = NULL; 1786 int s = pserialize_read_enter(); 1787 1788 ifp = if_byindex(imo->imo_multicast_if_index); 1789 if (ifp != NULL) { 1790 ia = in_get_ia_from_ifp(ifp); 1791 } 1792 addr = ia ? ia->ia_addr.sin_addr : zeroin_addr; 1793 pserialize_read_exit(s); 1794 } 1795 error = sockopt_set(sopt, &addr, sizeof(addr)); 1796 break; 1797 1798 case IP_MULTICAST_TTL: 1799 optval = imo ? imo->imo_multicast_ttl 1800 : IP_DEFAULT_MULTICAST_TTL; 1801 1802 error = sockopt_set(sopt, &optval, sizeof(optval)); 1803 break; 1804 1805 case IP_MULTICAST_LOOP: 1806 optval = imo ? imo->imo_multicast_loop 1807 : IP_DEFAULT_MULTICAST_LOOP; 1808 1809 error = sockopt_set(sopt, &optval, sizeof(optval)); 1810 break; 1811 1812 default: 1813 error = EOPNOTSUPP; 1814 } 1815 1816 return error; 1817 } 1818 1819 /* 1820 * Discard the IP multicast options. 1821 */ 1822 void 1823 ip_freemoptions(struct ip_moptions *imo) 1824 { 1825 int i; 1826 1827 if (imo != NULL) { 1828 for (i = 0; i < imo->imo_num_memberships; ++i) 1829 in_delmulti(imo->imo_membership[i]); 1830 kmem_free(imo, sizeof(*imo)); 1831 } 1832 } 1833 1834 /* 1835 * Routine called from ip_output() to loop back a copy of an IP multicast 1836 * packet to the input queue of a specified interface. Note that this 1837 * calls the output routine of the loopback "driver", but with an interface 1838 * pointer that might NOT be lo0ifp -- easier than replicating that code here. 1839 */ 1840 static void 1841 ip_mloopback(struct ifnet *ifp, struct mbuf *m, const struct sockaddr_in *dst) 1842 { 1843 struct ip *ip; 1844 struct mbuf *copym; 1845 1846 copym = m_copypacket(m, M_DONTWAIT); 1847 if (copym != NULL && 1848 (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip))) 1849 copym = m_pullup(copym, sizeof(struct ip)); 1850 if (copym == NULL) 1851 return; 1852 /* 1853 * We don't bother to fragment if the IP length is greater 1854 * than the interface's MTU. Can this possibly matter? 1855 */ 1856 ip = mtod(copym, struct ip *); 1857 1858 if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 1859 in_delayed_cksum(copym); 1860 copym->m_pkthdr.csum_flags &= 1861 ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 1862 } 1863 1864 ip->ip_sum = 0; 1865 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2); 1866 #ifndef NET_MPSAFE 1867 KERNEL_LOCK(1, NULL); 1868 #endif 1869 (void)looutput(ifp, copym, sintocsa(dst), NULL); 1870 #ifndef NET_MPSAFE 1871 KERNEL_UNLOCK_ONE(NULL); 1872 #endif 1873 } 1874 1875 /* 1876 * Ensure sending address is valid. 1877 * Returns 0 on success, -1 if an error should be sent back or 1 1878 * if the packet could be dropped without error (protocol dependent). 1879 */ 1880 static int 1881 ip_ifaddrvalid(const struct in_ifaddr *ia) 1882 { 1883 1884 if (ia == NULL) 1885 return -1; 1886 1887 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY) 1888 return 0; 1889 1890 if (ia->ia4_flags & IN_IFF_DUPLICATED) 1891 return -1; 1892 else if (ia->ia4_flags & (IN_IFF_TENTATIVE | IN_IFF_DETACHED)) 1893 return 1; 1894 1895 return 0; 1896 } 1897