1 /* $NetBSD: ip_output.c,v 1.269 2017/01/16 15:14:16 christos 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.269 2017/01/16 15:14:16 christos 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 s; 798 int error = 0; 799 800 ip = mtod(m, struct ip *); 801 hlen = ip->ip_hl << 2; 802 if (ifp != NULL) 803 sw_csum &= ~ifp->if_csum_flags_tx; 804 805 len = (mtu - hlen) &~ 7; 806 if (len < 8) { 807 m_freem(m); 808 return (EMSGSIZE); 809 } 810 811 firstlen = len; 812 mnext = &m->m_nextpkt; 813 814 /* 815 * Loop through length of segment after first fragment, 816 * make new header and copy data of each part and link onto chain. 817 */ 818 m0 = m; 819 mhlen = sizeof (struct ip); 820 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) { 821 MGETHDR(m, M_DONTWAIT, MT_HEADER); 822 if (m == 0) { 823 error = ENOBUFS; 824 IP_STATINC(IP_STAT_ODROPPED); 825 goto sendorfree; 826 } 827 MCLAIM(m, m0->m_owner); 828 *mnext = m; 829 mnext = &m->m_nextpkt; 830 m->m_data += max_linkhdr; 831 mhip = mtod(m, struct ip *); 832 *mhip = *ip; 833 /* we must inherit MCAST and BCAST flags */ 834 m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST); 835 if (hlen > sizeof (struct ip)) { 836 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 837 mhip->ip_hl = mhlen >> 2; 838 } 839 m->m_len = mhlen; 840 mhip->ip_off = ((off - hlen) >> 3) + 841 (ntohs(ip->ip_off) & ~IP_MF); 842 if (ip->ip_off & htons(IP_MF)) 843 mhip->ip_off |= IP_MF; 844 if (off + len >= ntohs(ip->ip_len)) 845 len = ntohs(ip->ip_len) - off; 846 else 847 mhip->ip_off |= IP_MF; 848 HTONS(mhip->ip_off); 849 mhip->ip_len = htons((u_int16_t)(len + mhlen)); 850 m->m_next = m_copym(m0, off, len, M_DONTWAIT); 851 if (m->m_next == 0) { 852 error = ENOBUFS; /* ??? */ 853 IP_STATINC(IP_STAT_ODROPPED); 854 goto sendorfree; 855 } 856 m->m_pkthdr.len = mhlen + len; 857 m_reset_rcvif(m); 858 mhip->ip_sum = 0; 859 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0); 860 if (sw_csum & M_CSUM_IPv4) { 861 mhip->ip_sum = in_cksum(m, mhlen); 862 } else { 863 /* 864 * checksum is hw-offloaded or not necessary. 865 */ 866 m->m_pkthdr.csum_flags |= 867 m0->m_pkthdr.csum_flags & M_CSUM_IPv4; 868 m->m_pkthdr.csum_data |= mhlen << 16; 869 KASSERT(!(ifp != NULL && 870 IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) || 871 (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0); 872 } 873 IP_STATINC(IP_STAT_OFRAGMENTS); 874 fragments++; 875 } 876 /* 877 * Update first fragment by trimming what's been copied out 878 * and updating header, then send each fragment (in order). 879 */ 880 m = m0; 881 m_adj(m, hlen + firstlen - ntohs(ip->ip_len)); 882 m->m_pkthdr.len = hlen + firstlen; 883 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len); 884 ip->ip_off |= htons(IP_MF); 885 ip->ip_sum = 0; 886 if (sw_csum & M_CSUM_IPv4) { 887 ip->ip_sum = in_cksum(m, hlen); 888 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4; 889 } else { 890 /* 891 * checksum is hw-offloaded or not necessary. 892 */ 893 KASSERT(!(ifp != NULL && IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) || 894 (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0); 895 KASSERT(M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data) >= 896 sizeof(struct ip)); 897 } 898 sendorfree: 899 /* 900 * If there is no room for all the fragments, don't queue 901 * any of them. 902 */ 903 if (ifp != NULL) { 904 s = splnet(); 905 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments && 906 error == 0) { 907 error = ENOBUFS; 908 IP_STATINC(IP_STAT_ODROPPED); 909 IFQ_INC_DROPS(&ifp->if_snd); 910 } 911 splx(s); 912 } 913 if (error) { 914 for (m = m0; m; m = m0) { 915 m0 = m->m_nextpkt; 916 m->m_nextpkt = NULL; 917 m_freem(m); 918 } 919 } 920 return (error); 921 } 922 923 /* 924 * Process a delayed payload checksum calculation. 925 */ 926 void 927 in_delayed_cksum(struct mbuf *m) 928 { 929 struct ip *ip; 930 u_int16_t csum, offset; 931 932 ip = mtod(m, struct ip *); 933 offset = ip->ip_hl << 2; 934 csum = in4_cksum(m, 0, offset, ntohs(ip->ip_len) - offset); 935 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv4) != 0) 936 csum = 0xffff; 937 938 offset += M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data); 939 940 if ((offset + sizeof(u_int16_t)) > m->m_len) { 941 /* This happen when ip options were inserted 942 printf("in_delayed_cksum: pullup len %d off %d proto %d\n", 943 m->m_len, offset, ip->ip_p); 944 */ 945 m_copyback(m, offset, sizeof(csum), (void *) &csum); 946 } else 947 *(u_int16_t *)(mtod(m, char *) + offset) = csum; 948 } 949 950 /* 951 * Determine the maximum length of the options to be inserted; 952 * we would far rather allocate too much space rather than too little. 953 */ 954 955 u_int 956 ip_optlen(struct inpcb *inp) 957 { 958 struct mbuf *m = inp->inp_options; 959 960 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst)) { 961 return (m->m_len - offsetof(struct ipoption, ipopt_dst)); 962 } 963 return 0; 964 } 965 966 /* 967 * Insert IP options into preformed packet. 968 * Adjust IP destination as required for IP source routing, 969 * as indicated by a non-zero in_addr at the start of the options. 970 */ 971 static struct mbuf * 972 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen) 973 { 974 struct ipoption *p = mtod(opt, struct ipoption *); 975 struct mbuf *n; 976 struct ip *ip = mtod(m, struct ip *); 977 unsigned optlen; 978 979 optlen = opt->m_len - sizeof(p->ipopt_dst); 980 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) 981 return (m); /* XXX should fail */ 982 if (!in_nullhost(p->ipopt_dst)) 983 ip->ip_dst = p->ipopt_dst; 984 if (M_READONLY(m) || M_LEADINGSPACE(m) < optlen) { 985 MGETHDR(n, M_DONTWAIT, MT_HEADER); 986 if (n == 0) 987 return (m); 988 MCLAIM(n, m->m_owner); 989 M_MOVE_PKTHDR(n, m); 990 m->m_len -= sizeof(struct ip); 991 m->m_data += sizeof(struct ip); 992 n->m_next = m; 993 m = n; 994 m->m_len = optlen + sizeof(struct ip); 995 m->m_data += max_linkhdr; 996 bcopy((void *)ip, mtod(m, void *), sizeof(struct ip)); 997 } else { 998 m->m_data -= optlen; 999 m->m_len += optlen; 1000 memmove(mtod(m, void *), ip, sizeof(struct ip)); 1001 } 1002 m->m_pkthdr.len += optlen; 1003 ip = mtod(m, struct ip *); 1004 bcopy((void *)p->ipopt_list, (void *)(ip + 1), (unsigned)optlen); 1005 *phlen = sizeof(struct ip) + optlen; 1006 ip->ip_len = htons(ntohs(ip->ip_len) + optlen); 1007 return (m); 1008 } 1009 1010 /* 1011 * Copy options from ip to jp, 1012 * omitting those not copied during fragmentation. 1013 */ 1014 int 1015 ip_optcopy(struct ip *ip, struct ip *jp) 1016 { 1017 u_char *cp, *dp; 1018 int opt, optlen, cnt; 1019 1020 cp = (u_char *)(ip + 1); 1021 dp = (u_char *)(jp + 1); 1022 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1023 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1024 opt = cp[0]; 1025 if (opt == IPOPT_EOL) 1026 break; 1027 if (opt == IPOPT_NOP) { 1028 /* Preserve for IP mcast tunnel's LSRR alignment. */ 1029 *dp++ = IPOPT_NOP; 1030 optlen = 1; 1031 continue; 1032 } 1033 1034 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp)); 1035 optlen = cp[IPOPT_OLEN]; 1036 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen < cnt); 1037 1038 /* Invalid lengths should have been caught by ip_dooptions. */ 1039 if (optlen > cnt) 1040 optlen = cnt; 1041 if (IPOPT_COPIED(opt)) { 1042 bcopy((void *)cp, (void *)dp, (unsigned)optlen); 1043 dp += optlen; 1044 } 1045 } 1046 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1047 *dp++ = IPOPT_EOL; 1048 return (optlen); 1049 } 1050 1051 /* 1052 * IP socket option processing. 1053 */ 1054 int 1055 ip_ctloutput(int op, struct socket *so, struct sockopt *sopt) 1056 { 1057 struct inpcb *inp = sotoinpcb(so); 1058 struct ip *ip = &inp->inp_ip; 1059 int inpflags = inp->inp_flags; 1060 int optval = 0, error = 0; 1061 1062 if (sopt->sopt_level != IPPROTO_IP) { 1063 if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) 1064 return 0; 1065 return ENOPROTOOPT; 1066 } 1067 1068 switch (op) { 1069 case PRCO_SETOPT: 1070 switch (sopt->sopt_name) { 1071 case IP_OPTIONS: 1072 #ifdef notyet 1073 case IP_RETOPTS: 1074 #endif 1075 error = ip_pcbopts(inp, sopt); 1076 break; 1077 1078 case IP_TOS: 1079 case IP_TTL: 1080 case IP_MINTTL: 1081 case IP_PKTINFO: 1082 case IP_RECVOPTS: 1083 case IP_RECVRETOPTS: 1084 case IP_RECVDSTADDR: 1085 case IP_RECVIF: 1086 case IP_RECVPKTINFO: 1087 case IP_RECVTTL: 1088 error = sockopt_getint(sopt, &optval); 1089 if (error) 1090 break; 1091 1092 switch (sopt->sopt_name) { 1093 case IP_TOS: 1094 ip->ip_tos = optval; 1095 break; 1096 1097 case IP_TTL: 1098 ip->ip_ttl = optval; 1099 break; 1100 1101 case IP_MINTTL: 1102 if (optval > 0 && optval <= MAXTTL) 1103 inp->inp_ip_minttl = optval; 1104 else 1105 error = EINVAL; 1106 break; 1107 #define OPTSET(bit) \ 1108 if (optval) \ 1109 inpflags |= bit; \ 1110 else \ 1111 inpflags &= ~bit; 1112 1113 case IP_PKTINFO: 1114 OPTSET(INP_PKTINFO); 1115 break; 1116 1117 case IP_RECVOPTS: 1118 OPTSET(INP_RECVOPTS); 1119 break; 1120 1121 case IP_RECVPKTINFO: 1122 OPTSET(INP_RECVPKTINFO); 1123 break; 1124 1125 case IP_RECVRETOPTS: 1126 OPTSET(INP_RECVRETOPTS); 1127 break; 1128 1129 case IP_RECVDSTADDR: 1130 OPTSET(INP_RECVDSTADDR); 1131 break; 1132 1133 case IP_RECVIF: 1134 OPTSET(INP_RECVIF); 1135 break; 1136 1137 case IP_RECVTTL: 1138 OPTSET(INP_RECVTTL); 1139 break; 1140 } 1141 break; 1142 #undef OPTSET 1143 1144 case IP_MULTICAST_IF: 1145 case IP_MULTICAST_TTL: 1146 case IP_MULTICAST_LOOP: 1147 case IP_ADD_MEMBERSHIP: 1148 case IP_DROP_MEMBERSHIP: 1149 error = ip_setmoptions(&inp->inp_moptions, sopt); 1150 break; 1151 1152 case IP_PORTRANGE: 1153 error = sockopt_getint(sopt, &optval); 1154 if (error) 1155 break; 1156 1157 switch (optval) { 1158 case IP_PORTRANGE_DEFAULT: 1159 case IP_PORTRANGE_HIGH: 1160 inpflags &= ~(INP_LOWPORT); 1161 break; 1162 1163 case IP_PORTRANGE_LOW: 1164 inpflags |= INP_LOWPORT; 1165 break; 1166 1167 default: 1168 error = EINVAL; 1169 break; 1170 } 1171 break; 1172 1173 case IP_PORTALGO: 1174 error = sockopt_getint(sopt, &optval); 1175 if (error) 1176 break; 1177 1178 error = portalgo_algo_index_select( 1179 (struct inpcb_hdr *)inp, optval); 1180 break; 1181 1182 #if defined(IPSEC) 1183 case IP_IPSEC_POLICY: 1184 if (ipsec_enabled) { 1185 error = ipsec4_set_policy(inp, sopt->sopt_name, 1186 sopt->sopt_data, sopt->sopt_size, 1187 curlwp->l_cred); 1188 break; 1189 } 1190 /*FALLTHROUGH*/ 1191 #endif /* IPSEC */ 1192 1193 default: 1194 error = ENOPROTOOPT; 1195 break; 1196 } 1197 break; 1198 1199 case PRCO_GETOPT: 1200 switch (sopt->sopt_name) { 1201 case IP_OPTIONS: 1202 case IP_RETOPTS: { 1203 struct mbuf *mopts = inp->inp_options; 1204 1205 if (mopts) { 1206 struct mbuf *m; 1207 1208 m = m_copym(mopts, 0, M_COPYALL, M_DONTWAIT); 1209 if (m == NULL) { 1210 error = ENOBUFS; 1211 break; 1212 } 1213 error = sockopt_setmbuf(sopt, m); 1214 } 1215 break; 1216 } 1217 case IP_PKTINFO: 1218 case IP_TOS: 1219 case IP_TTL: 1220 case IP_MINTTL: 1221 case IP_RECVOPTS: 1222 case IP_RECVRETOPTS: 1223 case IP_RECVDSTADDR: 1224 case IP_RECVIF: 1225 case IP_RECVPKTINFO: 1226 case IP_RECVTTL: 1227 case IP_ERRORMTU: 1228 switch (sopt->sopt_name) { 1229 case IP_TOS: 1230 optval = ip->ip_tos; 1231 break; 1232 1233 case IP_TTL: 1234 optval = ip->ip_ttl; 1235 break; 1236 1237 case IP_MINTTL: 1238 optval = inp->inp_ip_minttl; 1239 break; 1240 1241 case IP_ERRORMTU: 1242 optval = inp->inp_errormtu; 1243 break; 1244 1245 #define OPTBIT(bit) (inpflags & bit ? 1 : 0) 1246 1247 case IP_PKTINFO: 1248 optval = OPTBIT(INP_PKTINFO); 1249 break; 1250 1251 case IP_RECVOPTS: 1252 optval = OPTBIT(INP_RECVOPTS); 1253 break; 1254 1255 case IP_RECVPKTINFO: 1256 optval = OPTBIT(INP_RECVPKTINFO); 1257 break; 1258 1259 case IP_RECVRETOPTS: 1260 optval = OPTBIT(INP_RECVRETOPTS); 1261 break; 1262 1263 case IP_RECVDSTADDR: 1264 optval = OPTBIT(INP_RECVDSTADDR); 1265 break; 1266 1267 case IP_RECVIF: 1268 optval = OPTBIT(INP_RECVIF); 1269 break; 1270 1271 case IP_RECVTTL: 1272 optval = OPTBIT(INP_RECVTTL); 1273 break; 1274 } 1275 error = sockopt_setint(sopt, optval); 1276 break; 1277 1278 #if 0 /* defined(IPSEC) */ 1279 case IP_IPSEC_POLICY: 1280 { 1281 struct mbuf *m = NULL; 1282 1283 /* XXX this will return EINVAL as sopt is empty */ 1284 error = ipsec4_get_policy(inp, sopt->sopt_data, 1285 sopt->sopt_size, &m); 1286 if (error == 0) 1287 error = sockopt_setmbuf(sopt, m); 1288 break; 1289 } 1290 #endif /*IPSEC*/ 1291 1292 case IP_MULTICAST_IF: 1293 case IP_MULTICAST_TTL: 1294 case IP_MULTICAST_LOOP: 1295 case IP_ADD_MEMBERSHIP: 1296 case IP_DROP_MEMBERSHIP: 1297 error = ip_getmoptions(inp->inp_moptions, sopt); 1298 break; 1299 1300 case IP_PORTRANGE: 1301 if (inpflags & INP_LOWPORT) 1302 optval = IP_PORTRANGE_LOW; 1303 else 1304 optval = IP_PORTRANGE_DEFAULT; 1305 error = sockopt_setint(sopt, optval); 1306 break; 1307 1308 case IP_PORTALGO: 1309 optval = inp->inp_portalgo; 1310 error = sockopt_setint(sopt, optval); 1311 break; 1312 1313 default: 1314 error = ENOPROTOOPT; 1315 break; 1316 } 1317 break; 1318 } 1319 1320 if (!error) { 1321 inp->inp_flags = inpflags; 1322 } 1323 return error; 1324 } 1325 1326 /* 1327 * Set up IP options in pcb for insertion in output packets. 1328 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1329 * with destination address if source routed. 1330 */ 1331 static int 1332 ip_pcbopts(struct inpcb *inp, const struct sockopt *sopt) 1333 { 1334 struct mbuf *m; 1335 const u_char *cp; 1336 u_char *dp; 1337 int cnt; 1338 1339 /* Turn off any old options. */ 1340 if (inp->inp_options) { 1341 m_free(inp->inp_options); 1342 } 1343 inp->inp_options = NULL; 1344 if ((cnt = sopt->sopt_size) == 0) { 1345 /* Only turning off any previous options. */ 1346 return 0; 1347 } 1348 cp = sopt->sopt_data; 1349 1350 #ifndef __vax__ 1351 if (cnt % sizeof(int32_t)) 1352 return (EINVAL); 1353 #endif 1354 1355 m = m_get(M_DONTWAIT, MT_SOOPTS); 1356 if (m == NULL) 1357 return (ENOBUFS); 1358 1359 dp = mtod(m, u_char *); 1360 memset(dp, 0, sizeof(struct in_addr)); 1361 dp += sizeof(struct in_addr); 1362 m->m_len = sizeof(struct in_addr); 1363 1364 /* 1365 * IP option list according to RFC791. Each option is of the form 1366 * 1367 * [optval] [olen] [(olen - 2) data bytes] 1368 * 1369 * We validate the list and copy options to an mbuf for prepending 1370 * to data packets. The IP first-hop destination address will be 1371 * stored before actual options and is zero if unset. 1372 */ 1373 while (cnt > 0) { 1374 uint8_t optval, olen, offset; 1375 1376 optval = cp[IPOPT_OPTVAL]; 1377 1378 if (optval == IPOPT_EOL || optval == IPOPT_NOP) { 1379 olen = 1; 1380 } else { 1381 if (cnt < IPOPT_OLEN + 1) 1382 goto bad; 1383 1384 olen = cp[IPOPT_OLEN]; 1385 if (olen < IPOPT_OLEN + 1 || olen > cnt) 1386 goto bad; 1387 } 1388 1389 if (optval == IPOPT_LSRR || optval == IPOPT_SSRR) { 1390 /* 1391 * user process specifies route as: 1392 * ->A->B->C->D 1393 * D must be our final destination (but we can't 1394 * check that since we may not have connected yet). 1395 * A is first hop destination, which doesn't appear in 1396 * actual IP option, but is stored before the options. 1397 */ 1398 if (olen < IPOPT_OFFSET + 1 + sizeof(struct in_addr)) 1399 goto bad; 1400 1401 offset = cp[IPOPT_OFFSET]; 1402 memcpy(mtod(m, u_char *), cp + IPOPT_OFFSET + 1, 1403 sizeof(struct in_addr)); 1404 1405 cp += sizeof(struct in_addr); 1406 cnt -= sizeof(struct in_addr); 1407 olen -= sizeof(struct in_addr); 1408 1409 if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr)) 1410 goto bad; 1411 1412 memcpy(dp, cp, olen); 1413 dp[IPOPT_OPTVAL] = optval; 1414 dp[IPOPT_OLEN] = olen; 1415 dp[IPOPT_OFFSET] = offset; 1416 break; 1417 } else { 1418 if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr)) 1419 goto bad; 1420 1421 memcpy(dp, cp, olen); 1422 break; 1423 } 1424 1425 dp += olen; 1426 m->m_len += olen; 1427 1428 if (optval == IPOPT_EOL) 1429 break; 1430 1431 cp += olen; 1432 cnt -= olen; 1433 } 1434 1435 inp->inp_options = m; 1436 return 0; 1437 bad: 1438 (void)m_free(m); 1439 return EINVAL; 1440 } 1441 1442 /* 1443 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1444 */ 1445 static struct ifnet * 1446 ip_multicast_if(struct in_addr *a, int *ifindexp) 1447 { 1448 int ifindex; 1449 struct ifnet *ifp = NULL; 1450 struct in_ifaddr *ia; 1451 1452 if (ifindexp) 1453 *ifindexp = 0; 1454 if (ntohl(a->s_addr) >> 24 == 0) { 1455 ifindex = ntohl(a->s_addr) & 0xffffff; 1456 ifp = if_byindex(ifindex); 1457 if (!ifp) 1458 return NULL; 1459 if (ifindexp) 1460 *ifindexp = ifindex; 1461 } else { 1462 LIST_FOREACH(ia, &IN_IFADDR_HASH(a->s_addr), ia_hash) { 1463 if (in_hosteq(ia->ia_addr.sin_addr, *a) && 1464 (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) { 1465 ifp = ia->ia_ifp; 1466 break; 1467 } 1468 } 1469 } 1470 return ifp; 1471 } 1472 1473 static int 1474 ip_getoptval(const struct sockopt *sopt, u_int8_t *val, u_int maxval) 1475 { 1476 u_int tval; 1477 u_char cval; 1478 int error; 1479 1480 if (sopt == NULL) 1481 return EINVAL; 1482 1483 switch (sopt->sopt_size) { 1484 case sizeof(u_char): 1485 error = sockopt_get(sopt, &cval, sizeof(u_char)); 1486 tval = cval; 1487 break; 1488 1489 case sizeof(u_int): 1490 error = sockopt_get(sopt, &tval, sizeof(u_int)); 1491 break; 1492 1493 default: 1494 error = EINVAL; 1495 } 1496 1497 if (error) 1498 return error; 1499 1500 if (tval > maxval) 1501 return EINVAL; 1502 1503 *val = tval; 1504 return 0; 1505 } 1506 1507 static int 1508 ip_get_membership(const struct sockopt *sopt, struct ifnet **ifp, 1509 struct in_addr *ia, bool add) 1510 { 1511 int error; 1512 struct ip_mreq mreq; 1513 1514 error = sockopt_get(sopt, &mreq, sizeof(mreq)); 1515 if (error) 1516 return error; 1517 1518 if (!IN_MULTICAST(mreq.imr_multiaddr.s_addr)) 1519 return EINVAL; 1520 1521 memcpy(ia, &mreq.imr_multiaddr, sizeof(*ia)); 1522 1523 if (in_nullhost(mreq.imr_interface)) { 1524 union { 1525 struct sockaddr dst; 1526 struct sockaddr_in dst4; 1527 } u; 1528 struct route ro; 1529 1530 if (!add) { 1531 *ifp = NULL; 1532 return 0; 1533 } 1534 /* 1535 * If no interface address was provided, use the interface of 1536 * the route to the given multicast address. 1537 */ 1538 struct rtentry *rt; 1539 memset(&ro, 0, sizeof(ro)); 1540 1541 sockaddr_in_init(&u.dst4, ia, 0); 1542 error = rtcache_setdst(&ro, &u.dst); 1543 if (error != 0) 1544 return error; 1545 *ifp = (rt = rtcache_init(&ro)) != NULL ? rt->rt_ifp : NULL; 1546 rtcache_unref(rt, &ro); 1547 rtcache_free(&ro); 1548 } else { 1549 *ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1550 if (!add && *ifp == NULL) 1551 return EADDRNOTAVAIL; 1552 } 1553 return 0; 1554 } 1555 1556 /* 1557 * Add a multicast group membership. 1558 * Group must be a valid IP multicast address. 1559 */ 1560 static int 1561 ip_add_membership(struct ip_moptions *imo, const struct sockopt *sopt) 1562 { 1563 struct ifnet *ifp = NULL; // XXX: gcc [ppc] 1564 struct in_addr ia; 1565 int i, error; 1566 1567 if (sopt->sopt_size == sizeof(struct ip_mreq)) 1568 error = ip_get_membership(sopt, &ifp, &ia, true); 1569 else 1570 #ifdef INET6 1571 error = ip6_get_membership(sopt, &ifp, &ia, sizeof(ia)); 1572 #else 1573 return EINVAL; 1574 #endif 1575 1576 if (error) 1577 return error; 1578 1579 /* 1580 * See if we found an interface, and confirm that it 1581 * supports multicast. 1582 */ 1583 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) 1584 return EADDRNOTAVAIL; 1585 1586 /* 1587 * See if the membership already exists or if all the 1588 * membership slots are full. 1589 */ 1590 for (i = 0; i < imo->imo_num_memberships; ++i) { 1591 if (imo->imo_membership[i]->inm_ifp == ifp && 1592 in_hosteq(imo->imo_membership[i]->inm_addr, ia)) 1593 break; 1594 } 1595 if (i < imo->imo_num_memberships) 1596 return EADDRINUSE; 1597 1598 if (i == IP_MAX_MEMBERSHIPS) 1599 return ETOOMANYREFS; 1600 1601 /* 1602 * Everything looks good; add a new record to the multicast 1603 * address list for the given interface. 1604 */ 1605 if ((imo->imo_membership[i] = in_addmulti(&ia, ifp)) == NULL) 1606 return ENOBUFS; 1607 1608 ++imo->imo_num_memberships; 1609 return 0; 1610 } 1611 1612 /* 1613 * Drop a multicast group membership. 1614 * Group must be a valid IP multicast address. 1615 */ 1616 static int 1617 ip_drop_membership(struct ip_moptions *imo, const struct sockopt *sopt) 1618 { 1619 struct in_addr ia = { .s_addr = 0 }; // XXX: gcc [ppc] 1620 struct ifnet *ifp = NULL; // XXX: gcc [ppc] 1621 int i, error; 1622 1623 if (sopt->sopt_size == sizeof(struct ip_mreq)) 1624 error = ip_get_membership(sopt, &ifp, &ia, false); 1625 else 1626 #ifdef INET6 1627 error = ip6_get_membership(sopt, &ifp, &ia, sizeof(ia)); 1628 #else 1629 return EINVAL; 1630 #endif 1631 1632 if (error) 1633 return error; 1634 1635 /* 1636 * Find the membership in the membership array. 1637 */ 1638 for (i = 0; i < imo->imo_num_memberships; ++i) { 1639 if ((ifp == NULL || 1640 imo->imo_membership[i]->inm_ifp == ifp) && 1641 in_hosteq(imo->imo_membership[i]->inm_addr, ia)) 1642 break; 1643 } 1644 if (i == imo->imo_num_memberships) 1645 return EADDRNOTAVAIL; 1646 1647 /* 1648 * Give up the multicast address record to which the 1649 * membership points. 1650 */ 1651 in_delmulti(imo->imo_membership[i]); 1652 1653 /* 1654 * Remove the gap in the membership array. 1655 */ 1656 for (++i; i < imo->imo_num_memberships; ++i) 1657 imo->imo_membership[i-1] = imo->imo_membership[i]; 1658 --imo->imo_num_memberships; 1659 return 0; 1660 } 1661 1662 /* 1663 * Set the IP multicast options in response to user setsockopt(). 1664 */ 1665 int 1666 ip_setmoptions(struct ip_moptions **pimo, const struct sockopt *sopt) 1667 { 1668 struct ip_moptions *imo = *pimo; 1669 struct in_addr addr; 1670 struct ifnet *ifp; 1671 int ifindex, error = 0; 1672 1673 if (!imo) { 1674 /* 1675 * No multicast option buffer attached to the pcb; 1676 * allocate one and initialize to default values. 1677 */ 1678 imo = kmem_intr_alloc(sizeof(*imo), KM_NOSLEEP); 1679 if (imo == NULL) 1680 return ENOBUFS; 1681 1682 imo->imo_multicast_if_index = 0; 1683 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1684 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1685 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1686 imo->imo_num_memberships = 0; 1687 *pimo = imo; 1688 } 1689 1690 switch (sopt->sopt_name) { 1691 case IP_MULTICAST_IF: 1692 /* 1693 * Select the interface for outgoing multicast packets. 1694 */ 1695 error = sockopt_get(sopt, &addr, sizeof(addr)); 1696 if (error) 1697 break; 1698 1699 /* 1700 * INADDR_ANY is used to remove a previous selection. 1701 * When no interface is selected, a default one is 1702 * chosen every time a multicast packet is sent. 1703 */ 1704 if (in_nullhost(addr)) { 1705 imo->imo_multicast_if_index = 0; 1706 break; 1707 } 1708 /* 1709 * The selected interface is identified by its local 1710 * IP address. Find the interface and confirm that 1711 * it supports multicasting. 1712 */ 1713 ifp = ip_multicast_if(&addr, &ifindex); 1714 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1715 error = EADDRNOTAVAIL; 1716 break; 1717 } 1718 imo->imo_multicast_if_index = ifp->if_index; 1719 if (ifindex) 1720 imo->imo_multicast_addr = addr; 1721 else 1722 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1723 break; 1724 1725 case IP_MULTICAST_TTL: 1726 /* 1727 * Set the IP time-to-live for outgoing multicast packets. 1728 */ 1729 error = ip_getoptval(sopt, &imo->imo_multicast_ttl, MAXTTL); 1730 break; 1731 1732 case IP_MULTICAST_LOOP: 1733 /* 1734 * Set the loopback flag for outgoing multicast packets. 1735 * Must be zero or one. 1736 */ 1737 error = ip_getoptval(sopt, &imo->imo_multicast_loop, 1); 1738 break; 1739 1740 case IP_ADD_MEMBERSHIP: /* IPV6_JOIN_GROUP */ 1741 error = ip_add_membership(imo, sopt); 1742 break; 1743 1744 case IP_DROP_MEMBERSHIP: /* IPV6_LEAVE_GROUP */ 1745 error = ip_drop_membership(imo, sopt); 1746 break; 1747 1748 default: 1749 error = EOPNOTSUPP; 1750 break; 1751 } 1752 1753 /* 1754 * If all options have default values, no need to keep the mbuf. 1755 */ 1756 if (imo->imo_multicast_if_index == 0 && 1757 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1758 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1759 imo->imo_num_memberships == 0) { 1760 kmem_free(imo, sizeof(*imo)); 1761 *pimo = NULL; 1762 } 1763 1764 return error; 1765 } 1766 1767 /* 1768 * Return the IP multicast options in response to user getsockopt(). 1769 */ 1770 int 1771 ip_getmoptions(struct ip_moptions *imo, struct sockopt *sopt) 1772 { 1773 struct in_addr addr; 1774 uint8_t optval; 1775 int error = 0; 1776 1777 switch (sopt->sopt_name) { 1778 case IP_MULTICAST_IF: 1779 if (imo == NULL || imo->imo_multicast_if_index == 0) 1780 addr = zeroin_addr; 1781 else if (imo->imo_multicast_addr.s_addr) { 1782 /* return the value user has set */ 1783 addr = imo->imo_multicast_addr; 1784 } else { 1785 struct ifnet *ifp; 1786 struct in_ifaddr *ia = NULL; 1787 int s = pserialize_read_enter(); 1788 1789 ifp = if_byindex(imo->imo_multicast_if_index); 1790 if (ifp != NULL) { 1791 ia = in_get_ia_from_ifp(ifp); 1792 } 1793 addr = ia ? ia->ia_addr.sin_addr : zeroin_addr; 1794 pserialize_read_exit(s); 1795 } 1796 error = sockopt_set(sopt, &addr, sizeof(addr)); 1797 break; 1798 1799 case IP_MULTICAST_TTL: 1800 optval = imo ? imo->imo_multicast_ttl 1801 : IP_DEFAULT_MULTICAST_TTL; 1802 1803 error = sockopt_set(sopt, &optval, sizeof(optval)); 1804 break; 1805 1806 case IP_MULTICAST_LOOP: 1807 optval = imo ? imo->imo_multicast_loop 1808 : IP_DEFAULT_MULTICAST_LOOP; 1809 1810 error = sockopt_set(sopt, &optval, sizeof(optval)); 1811 break; 1812 1813 default: 1814 error = EOPNOTSUPP; 1815 } 1816 1817 return error; 1818 } 1819 1820 /* 1821 * Discard the IP multicast options. 1822 */ 1823 void 1824 ip_freemoptions(struct ip_moptions *imo) 1825 { 1826 int i; 1827 1828 if (imo != NULL) { 1829 for (i = 0; i < imo->imo_num_memberships; ++i) 1830 in_delmulti(imo->imo_membership[i]); 1831 kmem_free(imo, sizeof(*imo)); 1832 } 1833 } 1834 1835 /* 1836 * Routine called from ip_output() to loop back a copy of an IP multicast 1837 * packet to the input queue of a specified interface. Note that this 1838 * calls the output routine of the loopback "driver", but with an interface 1839 * pointer that might NOT be lo0ifp -- easier than replicating that code here. 1840 */ 1841 static void 1842 ip_mloopback(struct ifnet *ifp, struct mbuf *m, const struct sockaddr_in *dst) 1843 { 1844 struct ip *ip; 1845 struct mbuf *copym; 1846 1847 copym = m_copypacket(m, M_DONTWAIT); 1848 if (copym != NULL && 1849 (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip))) 1850 copym = m_pullup(copym, sizeof(struct ip)); 1851 if (copym == NULL) 1852 return; 1853 /* 1854 * We don't bother to fragment if the IP length is greater 1855 * than the interface's MTU. Can this possibly matter? 1856 */ 1857 ip = mtod(copym, struct ip *); 1858 1859 if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 1860 in_delayed_cksum(copym); 1861 copym->m_pkthdr.csum_flags &= 1862 ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 1863 } 1864 1865 ip->ip_sum = 0; 1866 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2); 1867 #ifndef NET_MPSAFE 1868 KERNEL_LOCK(1, NULL); 1869 #endif 1870 (void)looutput(ifp, copym, sintocsa(dst), NULL); 1871 #ifndef NET_MPSAFE 1872 KERNEL_UNLOCK_ONE(NULL); 1873 #endif 1874 } 1875 1876 /* 1877 * Ensure sending address is valid. 1878 * Returns 0 on success, -1 if an error should be sent back or 1 1879 * if the packet could be dropped without error (protocol dependent). 1880 */ 1881 static int 1882 ip_ifaddrvalid(const struct in_ifaddr *ia) 1883 { 1884 1885 if (ia == NULL) 1886 return -1; 1887 1888 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY) 1889 return 0; 1890 1891 if (ia->ia4_flags & IN_IFF_DUPLICATED) 1892 return -1; 1893 else if (ia->ia4_flags & (IN_IFF_TENTATIVE | IN_IFF_DETACHED)) 1894 return 1; 1895 1896 return 0; 1897 } 1898