1 /* $OpenBSD: ip_output.c,v 1.357 2020/06/24 22:03:43 cheloha Exp $ */ 2 /* $NetBSD: ip_output.c,v 1.28 1996/02/13 23:43:07 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 33 */ 34 35 #include "pf.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/mbuf.h> 40 #include <sys/protosw.h> 41 #include <sys/socket.h> 42 #include <sys/socketvar.h> 43 #include <sys/proc.h> 44 #include <sys/kernel.h> 45 46 #include <net/if.h> 47 #include <net/if_var.h> 48 #include <net/if_enc.h> 49 #include <net/route.h> 50 51 #include <netinet/in.h> 52 #include <netinet/ip.h> 53 #include <netinet/in_pcb.h> 54 #include <netinet/in_var.h> 55 #include <netinet/ip_var.h> 56 #include <netinet/ip_icmp.h> 57 #include <netinet/tcp.h> 58 #include <netinet/udp.h> 59 #include <netinet/tcp_timer.h> 60 #include <netinet/tcp_var.h> 61 #include <netinet/udp_var.h> 62 63 #if NPF > 0 64 #include <net/pfvar.h> 65 #endif 66 67 #ifdef IPSEC 68 #ifdef ENCDEBUG 69 #define DPRINTF(x) do { if (encdebug) printf x ; } while (0) 70 #else 71 #define DPRINTF(x) 72 #endif 73 #endif /* IPSEC */ 74 75 int ip_pcbopts(struct mbuf **, struct mbuf *); 76 int ip_setmoptions(int, struct ip_moptions **, struct mbuf *, u_int); 77 void ip_mloopback(struct ifnet *, struct mbuf *, struct sockaddr_in *); 78 static __inline u_int16_t __attribute__((__unused__)) 79 in_cksum_phdr(u_int32_t, u_int32_t, u_int32_t); 80 void in_delayed_cksum(struct mbuf *); 81 82 #ifdef IPSEC 83 struct tdb * 84 ip_output_ipsec_lookup(struct mbuf *m, int hlen, int *error, struct inpcb *inp, 85 int ipsecflowinfo); 86 int 87 ip_output_ipsec_send(struct tdb *, struct mbuf *, struct route *, int); 88 #endif /* IPSEC */ 89 90 /* 91 * IP output. The packet in mbuf chain m contains a skeletal IP 92 * header (with len, off, ttl, proto, tos, src, dst). 93 * The mbuf chain containing the packet will be freed. 94 * The mbuf opt, if present, will not be freed. 95 */ 96 int 97 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, int flags, 98 struct ip_moptions *imo, struct inpcb *inp, u_int32_t ipsecflowinfo) 99 { 100 struct ip *ip; 101 struct ifnet *ifp = NULL; 102 struct mbuf *m = m0; 103 int hlen = sizeof (struct ip); 104 int error = 0; 105 struct route iproute; 106 struct sockaddr_in *dst; 107 struct tdb *tdb = NULL; 108 u_long mtu; 109 #if defined(MROUTING) 110 int rv; 111 #endif 112 113 NET_ASSERT_LOCKED(); 114 115 #ifdef IPSEC 116 if (inp && (inp->inp_flags & INP_IPV6) != 0) 117 panic("ip_output: IPv6 pcb is passed"); 118 #endif /* IPSEC */ 119 120 #ifdef DIAGNOSTIC 121 if ((m->m_flags & M_PKTHDR) == 0) 122 panic("ip_output no HDR"); 123 #endif 124 if (opt) 125 m = ip_insertoptions(m, opt, &hlen); 126 127 ip = mtod(m, struct ip *); 128 129 /* 130 * Fill in IP header. 131 */ 132 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 133 ip->ip_v = IPVERSION; 134 ip->ip_off &= htons(IP_DF); 135 ip->ip_id = htons(ip_randomid()); 136 ip->ip_hl = hlen >> 2; 137 ipstat_inc(ips_localout); 138 } else { 139 hlen = ip->ip_hl << 2; 140 } 141 142 /* 143 * We should not send traffic to 0/8 say both Stevens and RFCs 144 * 5735 section 3 and 1122 sections 3.2.1.3 and 3.3.6. 145 */ 146 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == 0) { 147 error = ENETUNREACH; 148 goto bad; 149 } 150 151 #if NPF > 0 152 reroute: 153 #endif 154 155 /* 156 * Do a route lookup now in case we need the source address to 157 * do an SPD lookup in IPsec; for most packets, the source address 158 * is set at a higher level protocol. ICMPs and other packets 159 * though (e.g., traceroute) have a source address of zeroes. 160 */ 161 if (ro == NULL) { 162 ro = &iproute; 163 memset(ro, 0, sizeof(*ro)); 164 } 165 166 dst = satosin(&ro->ro_dst); 167 168 /* 169 * If there is a cached route, check that it is to the same 170 * destination and is still up. If not, free it and try again. 171 */ 172 if (!rtisvalid(ro->ro_rt) || 173 dst->sin_addr.s_addr != ip->ip_dst.s_addr || 174 ro->ro_tableid != m->m_pkthdr.ph_rtableid) { 175 rtfree(ro->ro_rt); 176 ro->ro_rt = NULL; 177 } 178 179 if (ro->ro_rt == NULL) { 180 dst->sin_family = AF_INET; 181 dst->sin_len = sizeof(*dst); 182 dst->sin_addr = ip->ip_dst; 183 ro->ro_tableid = m->m_pkthdr.ph_rtableid; 184 } 185 186 if ((IN_MULTICAST(ip->ip_dst.s_addr) || 187 (ip->ip_dst.s_addr == INADDR_BROADCAST)) && 188 imo != NULL && (ifp = if_get(imo->imo_ifidx)) != NULL) { 189 190 mtu = ifp->if_mtu; 191 if (ip->ip_src.s_addr == INADDR_ANY) { 192 struct in_ifaddr *ia; 193 194 IFP_TO_IA(ifp, ia); 195 if (ia != NULL) 196 ip->ip_src = ia->ia_addr.sin_addr; 197 } 198 } else { 199 struct in_ifaddr *ia; 200 201 if (ro->ro_rt == NULL) 202 ro->ro_rt = rtalloc_mpath(&ro->ro_dst, 203 &ip->ip_src.s_addr, ro->ro_tableid); 204 205 if (ro->ro_rt == NULL) { 206 ipstat_inc(ips_noroute); 207 error = EHOSTUNREACH; 208 goto bad; 209 } 210 211 ia = ifatoia(ro->ro_rt->rt_ifa); 212 if (ISSET(ro->ro_rt->rt_flags, RTF_LOCAL)) 213 ifp = if_get(rtable_loindex(m->m_pkthdr.ph_rtableid)); 214 else 215 ifp = if_get(ro->ro_rt->rt_ifidx); 216 /* 217 * We aren't using rtisvalid() here because the UP/DOWN state 218 * machine is broken with some Ethernet drivers like em(4). 219 * As a result we might try to use an invalid cached route 220 * entry while an interface is being detached. 221 */ 222 if (ifp == NULL) { 223 ipstat_inc(ips_noroute); 224 error = EHOSTUNREACH; 225 goto bad; 226 } 227 if ((mtu = ro->ro_rt->rt_mtu) == 0) 228 mtu = ifp->if_mtu; 229 230 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 231 dst = satosin(ro->ro_rt->rt_gateway); 232 233 /* Set the source IP address */ 234 if (ip->ip_src.s_addr == INADDR_ANY && ia) 235 ip->ip_src = ia->ia_addr.sin_addr; 236 } 237 238 #ifdef IPSEC 239 if (ipsec_in_use || inp != NULL) { 240 /* Do we have any pending SAs to apply ? */ 241 tdb = ip_output_ipsec_lookup(m, hlen, &error, inp, 242 ipsecflowinfo); 243 if (error != 0) { 244 /* Should silently drop packet */ 245 if (error == -EINVAL) 246 error = 0; 247 m_freem(m); 248 goto done; 249 } 250 if (tdb != NULL) { 251 /* 252 * If it needs TCP/UDP hardware-checksumming, do the 253 * computation now. 254 */ 255 in_proto_cksum_out(m, NULL); 256 } 257 } 258 #endif /* IPSEC */ 259 260 if (IN_MULTICAST(ip->ip_dst.s_addr) || 261 (ip->ip_dst.s_addr == INADDR_BROADCAST)) { 262 263 m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ? 264 M_BCAST : M_MCAST; 265 266 /* 267 * IP destination address is multicast. Make sure "dst" 268 * still points to the address in "ro". (It may have been 269 * changed to point to a gateway address, above.) 270 */ 271 dst = satosin(&ro->ro_dst); 272 273 /* 274 * See if the caller provided any multicast options 275 */ 276 if (imo != NULL) 277 ip->ip_ttl = imo->imo_ttl; 278 else 279 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 280 281 /* 282 * if we don't know the outgoing ifp yet, we can't generate 283 * output 284 */ 285 if (!ifp) { 286 ipstat_inc(ips_noroute); 287 error = EHOSTUNREACH; 288 goto bad; 289 } 290 291 /* 292 * Confirm that the outgoing interface supports multicast, 293 * but only if the packet actually is going out on that 294 * interface (i.e., no IPsec is applied). 295 */ 296 if ((((m->m_flags & M_MCAST) && 297 (ifp->if_flags & IFF_MULTICAST) == 0) || 298 ((m->m_flags & M_BCAST) && 299 (ifp->if_flags & IFF_BROADCAST) == 0)) && (tdb == NULL)) { 300 ipstat_inc(ips_noroute); 301 error = ENETUNREACH; 302 goto bad; 303 } 304 305 /* 306 * If source address not specified yet, use address 307 * of outgoing interface. 308 */ 309 if (ip->ip_src.s_addr == INADDR_ANY) { 310 struct in_ifaddr *ia; 311 312 IFP_TO_IA(ifp, ia); 313 if (ia != NULL) 314 ip->ip_src = ia->ia_addr.sin_addr; 315 } 316 317 if ((imo == NULL || imo->imo_loop) && 318 in_hasmulti(&ip->ip_dst, ifp)) { 319 /* 320 * If we belong to the destination multicast group 321 * on the outgoing interface, and the caller did not 322 * forbid loopback, loop back a copy. 323 * Can't defer TCP/UDP checksumming, do the 324 * computation now. 325 */ 326 in_proto_cksum_out(m, NULL); 327 ip_mloopback(ifp, m, dst); 328 } 329 #ifdef MROUTING 330 else { 331 /* 332 * If we are acting as a multicast router, perform 333 * multicast forwarding as if the packet had just 334 * arrived on the interface to which we are about 335 * to send. The multicast forwarding function 336 * recursively calls this function, using the 337 * IP_FORWARDING flag to prevent infinite recursion. 338 * 339 * Multicasts that are looped back by ip_mloopback(), 340 * above, will be forwarded by the ip_input() routine, 341 * if necessary. 342 */ 343 if (ipmforwarding && ip_mrouter[ifp->if_rdomain] && 344 (flags & IP_FORWARDING) == 0) { 345 KERNEL_LOCK(); 346 rv = ip_mforward(m, ifp); 347 KERNEL_UNLOCK(); 348 if (rv != 0) { 349 m_freem(m); 350 goto done; 351 } 352 } 353 } 354 #endif 355 /* 356 * Multicasts with a time-to-live of zero may be looped- 357 * back, above, but must not be transmitted on a network. 358 * Also, multicasts addressed to the loopback interface 359 * are not sent -- the above call to ip_mloopback() will 360 * loop back a copy if this host actually belongs to the 361 * destination group on the loopback interface. 362 */ 363 if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) { 364 m_freem(m); 365 goto done; 366 } 367 368 goto sendit; 369 } 370 371 /* 372 * Look for broadcast address and verify user is allowed to send 373 * such a packet; if the packet is going in an IPsec tunnel, skip 374 * this check. 375 */ 376 if ((tdb == NULL) && ((dst->sin_addr.s_addr == INADDR_BROADCAST) || 377 (ro && ro->ro_rt && ISSET(ro->ro_rt->rt_flags, RTF_BROADCAST)))) { 378 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 379 error = EADDRNOTAVAIL; 380 goto bad; 381 } 382 if ((flags & IP_ALLOWBROADCAST) == 0) { 383 error = EACCES; 384 goto bad; 385 } 386 387 /* Don't allow broadcast messages to be fragmented */ 388 if (ntohs(ip->ip_len) > ifp->if_mtu) { 389 error = EMSGSIZE; 390 goto bad; 391 } 392 m->m_flags |= M_BCAST; 393 } else 394 m->m_flags &= ~M_BCAST; 395 396 sendit: 397 /* 398 * If we're doing Path MTU discovery, we need to set DF unless 399 * the route's MTU is locked. 400 */ 401 if ((flags & IP_MTUDISC) && ro && ro->ro_rt && 402 (ro->ro_rt->rt_locks & RTV_MTU) == 0) 403 ip->ip_off |= htons(IP_DF); 404 405 #ifdef IPSEC 406 /* 407 * Check if the packet needs encapsulation. 408 */ 409 if (tdb != NULL) { 410 /* Callee frees mbuf */ 411 error = ip_output_ipsec_send(tdb, m, ro, 412 (flags & IP_FORWARDING) ? 1 : 0); 413 goto done; 414 } 415 #endif /* IPSEC */ 416 417 /* 418 * Packet filter 419 */ 420 #if NPF > 0 421 if (pf_test(AF_INET, (flags & IP_FORWARDING) ? PF_FWD : PF_OUT, 422 ifp, &m) != PF_PASS) { 423 error = EACCES; 424 m_freem(m); 425 goto done; 426 } 427 if (m == NULL) 428 goto done; 429 ip = mtod(m, struct ip *); 430 hlen = ip->ip_hl << 2; 431 if ((m->m_pkthdr.pf.flags & (PF_TAG_REROUTE | PF_TAG_GENERATED)) == 432 (PF_TAG_REROUTE | PF_TAG_GENERATED)) 433 /* already rerun the route lookup, go on */ 434 m->m_pkthdr.pf.flags &= ~(PF_TAG_GENERATED | PF_TAG_REROUTE); 435 else if (m->m_pkthdr.pf.flags & PF_TAG_REROUTE) { 436 /* tag as generated to skip over pf_test on rerun */ 437 m->m_pkthdr.pf.flags |= PF_TAG_GENERATED; 438 ro = NULL; 439 if_put(ifp); /* drop reference since target changed */ 440 ifp = NULL; 441 goto reroute; 442 } 443 #endif 444 in_proto_cksum_out(m, ifp); 445 446 #ifdef IPSEC 447 if (ipsec_in_use && (flags & IP_FORWARDING) && (ipforwarding == 2) && 448 (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) == NULL)) { 449 error = EHOSTUNREACH; 450 m_freem(m); 451 goto done; 452 } 453 #endif 454 455 /* 456 * If small enough for interface, can just send directly. 457 */ 458 if (ntohs(ip->ip_len) <= mtu) { 459 ip->ip_sum = 0; 460 if ((ifp->if_capabilities & IFCAP_CSUM_IPv4) && 461 (ifp->if_bridgeidx == 0)) 462 m->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT; 463 else { 464 ipstat_inc(ips_outswcsum); 465 ip->ip_sum = in_cksum(m, hlen); 466 } 467 468 error = ifp->if_output(ifp, m, sintosa(dst), ro->ro_rt); 469 goto done; 470 } 471 472 /* 473 * Too large for interface; fragment if possible. 474 * Must be able to put at least 8 bytes per fragment. 475 */ 476 if (ip->ip_off & htons(IP_DF)) { 477 #ifdef IPSEC 478 if (ip_mtudisc) 479 ipsec_adjust_mtu(m, ifp->if_mtu); 480 #endif 481 error = EMSGSIZE; 482 /* 483 * This case can happen if the user changed the MTU 484 * of an interface after enabling IP on it. Because 485 * most netifs don't keep track of routes pointing to 486 * them, there is no way for one to update all its 487 * routes when the MTU is changed. 488 */ 489 if (rtisvalid(ro->ro_rt) && 490 ISSET(ro->ro_rt->rt_flags, RTF_HOST) && 491 !(ro->ro_rt->rt_locks & RTV_MTU) && 492 (ro->ro_rt->rt_mtu > ifp->if_mtu)) { 493 ro->ro_rt->rt_mtu = ifp->if_mtu; 494 } 495 ipstat_inc(ips_cantfrag); 496 goto bad; 497 } 498 499 error = ip_fragment(m, ifp, mtu); 500 if (error) { 501 m = m0 = NULL; 502 goto bad; 503 } 504 505 for (; m; m = m0) { 506 m0 = m->m_nextpkt; 507 m->m_nextpkt = 0; 508 if (error == 0) 509 error = ifp->if_output(ifp, m, sintosa(dst), ro->ro_rt); 510 else 511 m_freem(m); 512 } 513 514 if (error == 0) 515 ipstat_inc(ips_fragmented); 516 517 done: 518 if (ro == &iproute && ro->ro_rt) 519 rtfree(ro->ro_rt); 520 if_put(ifp); 521 return (error); 522 bad: 523 m_freem(m0); 524 goto done; 525 } 526 527 #ifdef IPSEC 528 struct tdb * 529 ip_output_ipsec_lookup(struct mbuf *m, int hlen, int *error, struct inpcb *inp, 530 int ipsecflowinfo) 531 { 532 struct m_tag *mtag; 533 struct tdb_ident *tdbi; 534 struct tdb *tdb; 535 536 /* Do we have any pending SAs to apply ? */ 537 tdb = ipsp_spd_lookup(m, AF_INET, hlen, error, IPSP_DIRECTION_OUT, 538 NULL, inp, ipsecflowinfo); 539 if (tdb == NULL) 540 return NULL; 541 /* Loop detection */ 542 for (mtag = m_tag_first(m); mtag != NULL; mtag = m_tag_next(m, mtag)) { 543 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE) 544 continue; 545 tdbi = (struct tdb_ident *)(mtag + 1); 546 if (tdbi->spi == tdb->tdb_spi && 547 tdbi->proto == tdb->tdb_sproto && 548 tdbi->rdomain == tdb->tdb_rdomain && 549 !memcmp(&tdbi->dst, &tdb->tdb_dst, 550 sizeof(union sockaddr_union))) { 551 /* no IPsec needed */ 552 return NULL; 553 } 554 } 555 return tdb; 556 } 557 558 int 559 ip_output_ipsec_send(struct tdb *tdb, struct mbuf *m, struct route *ro, int fwd) 560 { 561 #if NPF > 0 562 struct ifnet *encif; 563 #endif 564 struct ip *ip; 565 int error; 566 567 #if NPF > 0 568 /* 569 * Packet filter 570 */ 571 if ((encif = enc_getif(tdb->tdb_rdomain, tdb->tdb_tap)) == NULL || 572 pf_test(AF_INET, fwd ? PF_FWD : PF_OUT, encif, &m) != PF_PASS) { 573 m_freem(m); 574 return EACCES; 575 } 576 if (m == NULL) 577 return 0; 578 /* 579 * PF_TAG_REROUTE handling or not... 580 * Packet is entering IPsec so the routing is 581 * already overruled by the IPsec policy. 582 * Until now the change was not reconsidered. 583 * What's the behaviour? 584 */ 585 in_proto_cksum_out(m, encif); 586 #endif 587 588 /* Check if we are allowed to fragment */ 589 ip = mtod(m, struct ip *); 590 if (ip_mtudisc && (ip->ip_off & htons(IP_DF)) && tdb->tdb_mtu && 591 ntohs(ip->ip_len) > tdb->tdb_mtu && 592 tdb->tdb_mtutimeout > gettime()) { 593 struct rtentry *rt = NULL; 594 int rt_mtucloned = 0; 595 int transportmode = 0; 596 597 transportmode = (tdb->tdb_dst.sa.sa_family == AF_INET) && 598 (tdb->tdb_dst.sin.sin_addr.s_addr == ip->ip_dst.s_addr); 599 600 /* Find a host route to store the mtu in */ 601 if (ro != NULL) 602 rt = ro->ro_rt; 603 /* but don't add a PMTU route for transport mode SAs */ 604 if (transportmode) 605 rt = NULL; 606 else if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0) { 607 rt = icmp_mtudisc_clone(ip->ip_dst, 608 m->m_pkthdr.ph_rtableid); 609 rt_mtucloned = 1; 610 } 611 DPRINTF(("%s: spi %08x mtu %d rt %p cloned %d\n", __func__, 612 ntohl(tdb->tdb_spi), tdb->tdb_mtu, rt, rt_mtucloned)); 613 if (rt != NULL) { 614 rt->rt_mtu = tdb->tdb_mtu; 615 if (ro && ro->ro_rt != NULL) { 616 rtfree(ro->ro_rt); 617 ro->ro_rt = rtalloc(&ro->ro_dst, RT_RESOLVE, 618 m->m_pkthdr.ph_rtableid); 619 } 620 if (rt_mtucloned) 621 rtfree(rt); 622 } 623 ipsec_adjust_mtu(m, tdb->tdb_mtu); 624 m_freem(m); 625 return EMSGSIZE; 626 } 627 628 /* 629 * Clear these -- they'll be set in the recursive invocation 630 * as needed. 631 */ 632 m->m_flags &= ~(M_MCAST | M_BCAST); 633 634 /* Callee frees mbuf */ 635 error = ipsp_process_packet(m, tdb, AF_INET, 0); 636 if (error) { 637 ipsecstat_inc(ipsec_odrops); 638 tdb->tdb_odrops++; 639 } 640 return error; 641 } 642 #endif /* IPSEC */ 643 644 int 645 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu) 646 { 647 struct ip *ip, *mhip; 648 struct mbuf *m0; 649 int len, hlen, off; 650 int mhlen, firstlen; 651 struct mbuf **mnext; 652 int fragments = 0; 653 int error = 0; 654 655 ip = mtod(m, struct ip *); 656 hlen = ip->ip_hl << 2; 657 658 len = (mtu - hlen) &~ 7; 659 if (len < 8) { 660 m_freem(m); 661 return (EMSGSIZE); 662 } 663 664 /* 665 * If we are doing fragmentation, we can't defer TCP/UDP 666 * checksumming; compute the checksum and clear the flag. 667 */ 668 in_proto_cksum_out(m, NULL); 669 firstlen = len; 670 mnext = &m->m_nextpkt; 671 672 /* 673 * Loop through length of segment after first fragment, 674 * make new header and copy data of each part and link onto chain. 675 */ 676 m0 = m; 677 mhlen = sizeof (struct ip); 678 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) { 679 MGETHDR(m, M_DONTWAIT, MT_HEADER); 680 if (m == NULL) { 681 ipstat_inc(ips_odropped); 682 error = ENOBUFS; 683 goto sendorfree; 684 } 685 *mnext = m; 686 mnext = &m->m_nextpkt; 687 m->m_data += max_linkhdr; 688 mhip = mtod(m, struct ip *); 689 *mhip = *ip; 690 /* we must inherit MCAST/BCAST flags, routing table and prio */ 691 m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST); 692 m->m_pkthdr.ph_rtableid = m0->m_pkthdr.ph_rtableid; 693 m->m_pkthdr.pf.prio = m0->m_pkthdr.pf.prio; 694 if (hlen > sizeof (struct ip)) { 695 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 696 mhip->ip_hl = mhlen >> 2; 697 } 698 m->m_len = mhlen; 699 mhip->ip_off = ((off - hlen) >> 3) + 700 (ntohs(ip->ip_off) & ~IP_MF); 701 if (ip->ip_off & htons(IP_MF)) 702 mhip->ip_off |= IP_MF; 703 if (off + len >= ntohs(ip->ip_len)) 704 len = ntohs(ip->ip_len) - off; 705 else 706 mhip->ip_off |= IP_MF; 707 mhip->ip_len = htons((u_int16_t)(len + mhlen)); 708 m->m_next = m_copym(m0, off, len, M_NOWAIT); 709 if (m->m_next == 0) { 710 ipstat_inc(ips_odropped); 711 error = ENOBUFS; 712 goto sendorfree; 713 } 714 m->m_pkthdr.len = mhlen + len; 715 m->m_pkthdr.ph_ifidx = 0; 716 mhip->ip_off = htons((u_int16_t)mhip->ip_off); 717 mhip->ip_sum = 0; 718 if ((ifp != NULL) && 719 (ifp->if_capabilities & IFCAP_CSUM_IPv4) && 720 (ifp->if_bridgeidx == 0)) 721 m->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT; 722 else { 723 ipstat_inc(ips_outswcsum); 724 mhip->ip_sum = in_cksum(m, mhlen); 725 } 726 ipstat_inc(ips_ofragments); 727 fragments++; 728 } 729 /* 730 * Update first fragment by trimming what's been copied out 731 * and updating header, then send each fragment (in order). 732 */ 733 m = m0; 734 m_adj(m, hlen + firstlen - ntohs(ip->ip_len)); 735 m->m_pkthdr.len = hlen + firstlen; 736 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len); 737 ip->ip_off |= htons(IP_MF); 738 ip->ip_sum = 0; 739 if ((ifp != NULL) && 740 (ifp->if_capabilities & IFCAP_CSUM_IPv4) && 741 (ifp->if_bridgeidx == 0)) 742 m->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT; 743 else { 744 ipstat_inc(ips_outswcsum); 745 ip->ip_sum = in_cksum(m, hlen); 746 } 747 sendorfree: 748 if (error) { 749 for (m = m0; m; m = m0) { 750 m0 = m->m_nextpkt; 751 m->m_nextpkt = NULL; 752 m_freem(m); 753 } 754 } 755 756 return (error); 757 } 758 759 /* 760 * Insert IP options into preformed packet. 761 * Adjust IP destination as required for IP source routing, 762 * as indicated by a non-zero in_addr at the start of the options. 763 */ 764 struct mbuf * 765 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen) 766 { 767 struct ipoption *p = mtod(opt, struct ipoption *); 768 struct mbuf *n; 769 struct ip *ip = mtod(m, struct ip *); 770 unsigned int optlen; 771 772 optlen = opt->m_len - sizeof(p->ipopt_dst); 773 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) 774 return (m); /* XXX should fail */ 775 if (p->ipopt_dst.s_addr) 776 ip->ip_dst = p->ipopt_dst; 777 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 778 MGETHDR(n, M_DONTWAIT, MT_HEADER); 779 if (n == NULL) 780 return (m); 781 M_MOVE_HDR(n, m); 782 n->m_pkthdr.len += optlen; 783 m->m_len -= sizeof(struct ip); 784 m->m_data += sizeof(struct ip); 785 n->m_next = m; 786 m = n; 787 m->m_len = optlen + sizeof(struct ip); 788 m->m_data += max_linkhdr; 789 memcpy(mtod(m, caddr_t), ip, sizeof(struct ip)); 790 } else { 791 m->m_data -= optlen; 792 m->m_len += optlen; 793 m->m_pkthdr.len += optlen; 794 memmove(mtod(m, caddr_t), (caddr_t)ip, sizeof(struct ip)); 795 } 796 ip = mtod(m, struct ip *); 797 memcpy(ip + 1, p->ipopt_list, optlen); 798 *phlen = sizeof(struct ip) + optlen; 799 ip->ip_len = htons(ntohs(ip->ip_len) + optlen); 800 return (m); 801 } 802 803 /* 804 * Copy options from ip to jp, 805 * omitting those not copied during fragmentation. 806 */ 807 int 808 ip_optcopy(struct ip *ip, struct ip *jp) 809 { 810 u_char *cp, *dp; 811 int opt, optlen, cnt; 812 813 cp = (u_char *)(ip + 1); 814 dp = (u_char *)(jp + 1); 815 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 816 for (; cnt > 0; cnt -= optlen, cp += optlen) { 817 opt = cp[0]; 818 if (opt == IPOPT_EOL) 819 break; 820 if (opt == IPOPT_NOP) { 821 /* Preserve for IP mcast tunnel's LSRR alignment. */ 822 *dp++ = IPOPT_NOP; 823 optlen = 1; 824 continue; 825 } 826 #ifdef DIAGNOSTIC 827 if (cnt < IPOPT_OLEN + sizeof(*cp)) 828 panic("malformed IPv4 option passed to ip_optcopy"); 829 #endif 830 optlen = cp[IPOPT_OLEN]; 831 #ifdef DIAGNOSTIC 832 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 833 panic("malformed IPv4 option passed to ip_optcopy"); 834 #endif 835 /* bogus lengths should have been caught by ip_dooptions */ 836 if (optlen > cnt) 837 optlen = cnt; 838 if (IPOPT_COPIED(opt)) { 839 memcpy(dp, cp, optlen); 840 dp += optlen; 841 } 842 } 843 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 844 *dp++ = IPOPT_EOL; 845 return (optlen); 846 } 847 848 /* 849 * IP socket option processing. 850 */ 851 int 852 ip_ctloutput(int op, struct socket *so, int level, int optname, 853 struct mbuf *m) 854 { 855 struct inpcb *inp = sotoinpcb(so); 856 int optval = 0; 857 struct proc *p = curproc; /* XXX */ 858 int error = 0; 859 u_int rtid = 0; 860 861 if (level != IPPROTO_IP) 862 return (EINVAL); 863 864 switch (op) { 865 case PRCO_SETOPT: 866 switch (optname) { 867 case IP_OPTIONS: 868 return (ip_pcbopts(&inp->inp_options, m)); 869 870 case IP_TOS: 871 case IP_TTL: 872 case IP_MINTTL: 873 case IP_RECVOPTS: 874 case IP_RECVRETOPTS: 875 case IP_RECVDSTADDR: 876 case IP_RECVIF: 877 case IP_RECVTTL: 878 case IP_RECVDSTPORT: 879 case IP_RECVRTABLE: 880 case IP_IPSECFLOWINFO: 881 if (m == NULL || m->m_len != sizeof(int)) 882 error = EINVAL; 883 else { 884 optval = *mtod(m, int *); 885 switch (optname) { 886 887 case IP_TOS: 888 inp->inp_ip.ip_tos = optval; 889 break; 890 891 case IP_TTL: 892 if (optval > 0 && optval <= MAXTTL) 893 inp->inp_ip.ip_ttl = optval; 894 else if (optval == -1) 895 inp->inp_ip.ip_ttl = ip_defttl; 896 else 897 error = EINVAL; 898 break; 899 900 case IP_MINTTL: 901 if (optval >= 0 && optval <= MAXTTL) 902 inp->inp_ip_minttl = optval; 903 else 904 error = EINVAL; 905 break; 906 #define OPTSET(bit) \ 907 if (optval) \ 908 inp->inp_flags |= bit; \ 909 else \ 910 inp->inp_flags &= ~bit; 911 912 case IP_RECVOPTS: 913 OPTSET(INP_RECVOPTS); 914 break; 915 916 case IP_RECVRETOPTS: 917 OPTSET(INP_RECVRETOPTS); 918 break; 919 920 case IP_RECVDSTADDR: 921 OPTSET(INP_RECVDSTADDR); 922 break; 923 case IP_RECVIF: 924 OPTSET(INP_RECVIF); 925 break; 926 case IP_RECVTTL: 927 OPTSET(INP_RECVTTL); 928 break; 929 case IP_RECVDSTPORT: 930 OPTSET(INP_RECVDSTPORT); 931 break; 932 case IP_RECVRTABLE: 933 OPTSET(INP_RECVRTABLE); 934 break; 935 case IP_IPSECFLOWINFO: 936 OPTSET(INP_IPSECFLOWINFO); 937 break; 938 } 939 } 940 break; 941 #undef OPTSET 942 943 case IP_MULTICAST_IF: 944 case IP_MULTICAST_TTL: 945 case IP_MULTICAST_LOOP: 946 case IP_ADD_MEMBERSHIP: 947 case IP_DROP_MEMBERSHIP: 948 error = ip_setmoptions(optname, &inp->inp_moptions, m, 949 inp->inp_rtableid); 950 break; 951 952 case IP_PORTRANGE: 953 if (m == NULL || m->m_len != sizeof(int)) 954 error = EINVAL; 955 else { 956 optval = *mtod(m, int *); 957 958 switch (optval) { 959 960 case IP_PORTRANGE_DEFAULT: 961 inp->inp_flags &= ~(INP_LOWPORT); 962 inp->inp_flags &= ~(INP_HIGHPORT); 963 break; 964 965 case IP_PORTRANGE_HIGH: 966 inp->inp_flags &= ~(INP_LOWPORT); 967 inp->inp_flags |= INP_HIGHPORT; 968 break; 969 970 case IP_PORTRANGE_LOW: 971 inp->inp_flags &= ~(INP_HIGHPORT); 972 inp->inp_flags |= INP_LOWPORT; 973 break; 974 975 default: 976 977 error = EINVAL; 978 break; 979 } 980 } 981 break; 982 case IP_AUTH_LEVEL: 983 case IP_ESP_TRANS_LEVEL: 984 case IP_ESP_NETWORK_LEVEL: 985 case IP_IPCOMP_LEVEL: 986 #ifndef IPSEC 987 error = EOPNOTSUPP; 988 #else 989 if (m == NULL || m->m_len != sizeof(int)) { 990 error = EINVAL; 991 break; 992 } 993 optval = *mtod(m, int *); 994 995 if (optval < IPSEC_LEVEL_BYPASS || 996 optval > IPSEC_LEVEL_UNIQUE) { 997 error = EINVAL; 998 break; 999 } 1000 1001 switch (optname) { 1002 case IP_AUTH_LEVEL: 1003 if (optval < IPSEC_AUTH_LEVEL_DEFAULT && 1004 suser(p)) { 1005 error = EACCES; 1006 break; 1007 } 1008 inp->inp_seclevel[SL_AUTH] = optval; 1009 break; 1010 1011 case IP_ESP_TRANS_LEVEL: 1012 if (optval < IPSEC_ESP_TRANS_LEVEL_DEFAULT && 1013 suser(p)) { 1014 error = EACCES; 1015 break; 1016 } 1017 inp->inp_seclevel[SL_ESP_TRANS] = optval; 1018 break; 1019 1020 case IP_ESP_NETWORK_LEVEL: 1021 if (optval < IPSEC_ESP_NETWORK_LEVEL_DEFAULT && 1022 suser(p)) { 1023 error = EACCES; 1024 break; 1025 } 1026 inp->inp_seclevel[SL_ESP_NETWORK] = optval; 1027 break; 1028 case IP_IPCOMP_LEVEL: 1029 if (optval < IPSEC_IPCOMP_LEVEL_DEFAULT && 1030 suser(p)) { 1031 error = EACCES; 1032 break; 1033 } 1034 inp->inp_seclevel[SL_IPCOMP] = optval; 1035 break; 1036 } 1037 #endif 1038 break; 1039 1040 case IP_IPSEC_LOCAL_ID: 1041 case IP_IPSEC_REMOTE_ID: 1042 error = EOPNOTSUPP; 1043 break; 1044 case SO_RTABLE: 1045 if (m == NULL || m->m_len < sizeof(u_int)) { 1046 error = EINVAL; 1047 break; 1048 } 1049 rtid = *mtod(m, u_int *); 1050 if (inp->inp_rtableid == rtid) 1051 break; 1052 /* needs privileges to switch when already set */ 1053 if (p->p_p->ps_rtableid != rtid && 1054 p->p_p->ps_rtableid != 0 && 1055 (error = suser(p)) != 0) 1056 break; 1057 /* table must exist */ 1058 if (!rtable_exists(rtid)) { 1059 error = EINVAL; 1060 break; 1061 } 1062 if (inp->inp_lport) { 1063 error = EBUSY; 1064 break; 1065 } 1066 inp->inp_rtableid = rtid; 1067 in_pcbrehash(inp); 1068 break; 1069 case IP_PIPEX: 1070 if (m != NULL && m->m_len == sizeof(int)) 1071 inp->inp_pipex = *mtod(m, int *); 1072 else 1073 error = EINVAL; 1074 break; 1075 1076 default: 1077 error = ENOPROTOOPT; 1078 break; 1079 } 1080 break; 1081 1082 case PRCO_GETOPT: 1083 switch (optname) { 1084 case IP_OPTIONS: 1085 case IP_RETOPTS: 1086 if (inp->inp_options) { 1087 m->m_len = inp->inp_options->m_len; 1088 memcpy(mtod(m, caddr_t), 1089 mtod(inp->inp_options, caddr_t), m->m_len); 1090 } else 1091 m->m_len = 0; 1092 break; 1093 1094 case IP_TOS: 1095 case IP_TTL: 1096 case IP_MINTTL: 1097 case IP_RECVOPTS: 1098 case IP_RECVRETOPTS: 1099 case IP_RECVDSTADDR: 1100 case IP_RECVIF: 1101 case IP_RECVTTL: 1102 case IP_RECVDSTPORT: 1103 case IP_RECVRTABLE: 1104 case IP_IPSECFLOWINFO: 1105 case IP_IPDEFTTL: 1106 m->m_len = sizeof(int); 1107 switch (optname) { 1108 1109 case IP_TOS: 1110 optval = inp->inp_ip.ip_tos; 1111 break; 1112 1113 case IP_TTL: 1114 optval = inp->inp_ip.ip_ttl; 1115 break; 1116 1117 case IP_MINTTL: 1118 optval = inp->inp_ip_minttl; 1119 break; 1120 1121 case IP_IPDEFTTL: 1122 optval = ip_defttl; 1123 break; 1124 1125 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1126 1127 case IP_RECVOPTS: 1128 optval = OPTBIT(INP_RECVOPTS); 1129 break; 1130 1131 case IP_RECVRETOPTS: 1132 optval = OPTBIT(INP_RECVRETOPTS); 1133 break; 1134 1135 case IP_RECVDSTADDR: 1136 optval = OPTBIT(INP_RECVDSTADDR); 1137 break; 1138 case IP_RECVIF: 1139 optval = OPTBIT(INP_RECVIF); 1140 break; 1141 case IP_RECVTTL: 1142 optval = OPTBIT(INP_RECVTTL); 1143 break; 1144 case IP_RECVDSTPORT: 1145 optval = OPTBIT(INP_RECVDSTPORT); 1146 break; 1147 case IP_RECVRTABLE: 1148 optval = OPTBIT(INP_RECVRTABLE); 1149 break; 1150 case IP_IPSECFLOWINFO: 1151 optval = OPTBIT(INP_IPSECFLOWINFO); 1152 break; 1153 } 1154 *mtod(m, int *) = optval; 1155 break; 1156 1157 case IP_MULTICAST_IF: 1158 case IP_MULTICAST_TTL: 1159 case IP_MULTICAST_LOOP: 1160 case IP_ADD_MEMBERSHIP: 1161 case IP_DROP_MEMBERSHIP: 1162 error = ip_getmoptions(optname, inp->inp_moptions, m); 1163 break; 1164 1165 case IP_PORTRANGE: 1166 m->m_len = sizeof(int); 1167 1168 if (inp->inp_flags & INP_HIGHPORT) 1169 optval = IP_PORTRANGE_HIGH; 1170 else if (inp->inp_flags & INP_LOWPORT) 1171 optval = IP_PORTRANGE_LOW; 1172 else 1173 optval = 0; 1174 1175 *mtod(m, int *) = optval; 1176 break; 1177 1178 case IP_AUTH_LEVEL: 1179 case IP_ESP_TRANS_LEVEL: 1180 case IP_ESP_NETWORK_LEVEL: 1181 case IP_IPCOMP_LEVEL: 1182 #ifndef IPSEC 1183 m->m_len = sizeof(int); 1184 *mtod(m, int *) = IPSEC_LEVEL_NONE; 1185 #else 1186 m->m_len = sizeof(int); 1187 switch (optname) { 1188 case IP_AUTH_LEVEL: 1189 optval = inp->inp_seclevel[SL_AUTH]; 1190 break; 1191 1192 case IP_ESP_TRANS_LEVEL: 1193 optval = inp->inp_seclevel[SL_ESP_TRANS]; 1194 break; 1195 1196 case IP_ESP_NETWORK_LEVEL: 1197 optval = inp->inp_seclevel[SL_ESP_NETWORK]; 1198 break; 1199 case IP_IPCOMP_LEVEL: 1200 optval = inp->inp_seclevel[SL_IPCOMP]; 1201 break; 1202 } 1203 *mtod(m, int *) = optval; 1204 #endif 1205 break; 1206 case IP_IPSEC_LOCAL_ID: 1207 case IP_IPSEC_REMOTE_ID: 1208 error = EOPNOTSUPP; 1209 break; 1210 case SO_RTABLE: 1211 m->m_len = sizeof(u_int); 1212 *mtod(m, u_int *) = inp->inp_rtableid; 1213 break; 1214 case IP_PIPEX: 1215 m->m_len = sizeof(int); 1216 *mtod(m, int *) = inp->inp_pipex; 1217 break; 1218 default: 1219 error = ENOPROTOOPT; 1220 break; 1221 } 1222 break; 1223 } 1224 return (error); 1225 } 1226 1227 /* 1228 * Set up IP options in pcb for insertion in output packets. 1229 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1230 * with destination address if source routed. 1231 */ 1232 int 1233 ip_pcbopts(struct mbuf **pcbopt, struct mbuf *m) 1234 { 1235 struct mbuf *n; 1236 struct ipoption *p; 1237 int cnt, off, optlen; 1238 u_char *cp; 1239 u_char opt; 1240 1241 /* turn off any old options */ 1242 m_freem(*pcbopt); 1243 *pcbopt = NULL; 1244 if (m == NULL || m->m_len == 0) { 1245 /* 1246 * Only turning off any previous options. 1247 */ 1248 return (0); 1249 } 1250 1251 if (m->m_len % sizeof(int32_t) || 1252 m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1253 return (EINVAL); 1254 1255 /* Don't sleep because NET_LOCK() is hold. */ 1256 if ((n = m_get(M_NOWAIT, MT_SOOPTS)) == NULL) 1257 return (ENOBUFS); 1258 p = mtod(n, struct ipoption *); 1259 memset(p, 0, sizeof (*p)); /* 0 = IPOPT_EOL, needed for padding */ 1260 n->m_len = sizeof(struct in_addr); 1261 1262 off = 0; 1263 cnt = m->m_len; 1264 cp = mtod(m, u_char *); 1265 1266 while (cnt > 0) { 1267 opt = cp[IPOPT_OPTVAL]; 1268 1269 if (opt == IPOPT_NOP || opt == IPOPT_EOL) { 1270 optlen = 1; 1271 } else { 1272 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1273 goto bad; 1274 optlen = cp[IPOPT_OLEN]; 1275 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1276 goto bad; 1277 } 1278 switch (opt) { 1279 default: 1280 memcpy(p->ipopt_list + off, cp, optlen); 1281 break; 1282 1283 case IPOPT_LSRR: 1284 case IPOPT_SSRR: 1285 /* 1286 * user process specifies route as: 1287 * ->A->B->C->D 1288 * D must be our final destination (but we can't 1289 * check that since we may not have connected yet). 1290 * A is first hop destination, which doesn't appear in 1291 * actual IP option, but is stored before the options. 1292 */ 1293 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1294 goto bad; 1295 1296 /* 1297 * Optlen is smaller because first address is popped. 1298 * Cnt and cp will be adjusted a bit later to reflect 1299 * this. 1300 */ 1301 optlen -= sizeof(struct in_addr); 1302 p->ipopt_list[off + IPOPT_OPTVAL] = opt; 1303 p->ipopt_list[off + IPOPT_OLEN] = optlen; 1304 1305 /* 1306 * Move first hop before start of options. 1307 */ 1308 memcpy(&p->ipopt_dst, cp + IPOPT_OFFSET, 1309 sizeof(struct in_addr)); 1310 cp += sizeof(struct in_addr); 1311 cnt -= sizeof(struct in_addr); 1312 /* 1313 * Then copy rest of options 1314 */ 1315 memcpy(p->ipopt_list + off + IPOPT_OFFSET, 1316 cp + IPOPT_OFFSET, optlen - IPOPT_OFFSET); 1317 break; 1318 } 1319 off += optlen; 1320 cp += optlen; 1321 cnt -= optlen; 1322 1323 if (opt == IPOPT_EOL) 1324 break; 1325 } 1326 /* pad options to next word, since p was zeroed just adjust off */ 1327 off = (off + sizeof(int32_t) - 1) & ~(sizeof(int32_t) - 1); 1328 n->m_len += off; 1329 if (n->m_len > sizeof(*p)) { 1330 bad: 1331 m_freem(n); 1332 return (EINVAL); 1333 } 1334 1335 *pcbopt = n; 1336 return (0); 1337 } 1338 1339 /* 1340 * Set the IP multicast options in response to user setsockopt(). 1341 */ 1342 int 1343 ip_setmoptions(int optname, struct ip_moptions **imop, struct mbuf *m, 1344 u_int rtableid) 1345 { 1346 struct in_addr addr; 1347 struct in_ifaddr *ia; 1348 struct ip_mreq *mreq; 1349 struct ifnet *ifp = NULL; 1350 struct ip_moptions *imo = *imop; 1351 struct in_multi **immp; 1352 struct rtentry *rt; 1353 struct sockaddr_in sin; 1354 int i, error = 0; 1355 u_char loop; 1356 1357 if (imo == NULL) { 1358 /* 1359 * No multicast option buffer attached to the pcb; 1360 * allocate one and initialize to default values. 1361 */ 1362 imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK|M_ZERO); 1363 immp = mallocarray(IP_MIN_MEMBERSHIPS, sizeof(*immp), M_IPMOPTS, 1364 M_WAITOK|M_ZERO); 1365 *imop = imo; 1366 imo->imo_ifidx = 0; 1367 imo->imo_ttl = IP_DEFAULT_MULTICAST_TTL; 1368 imo->imo_loop = IP_DEFAULT_MULTICAST_LOOP; 1369 imo->imo_num_memberships = 0; 1370 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS; 1371 imo->imo_membership = immp; 1372 } 1373 1374 switch (optname) { 1375 1376 case IP_MULTICAST_IF: 1377 /* 1378 * Select the interface for outgoing multicast packets. 1379 */ 1380 if (m == NULL || m->m_len != sizeof(struct in_addr)) { 1381 error = EINVAL; 1382 break; 1383 } 1384 addr = *(mtod(m, struct in_addr *)); 1385 /* 1386 * INADDR_ANY is used to remove a previous selection. 1387 * When no interface is selected, a default one is 1388 * chosen every time a multicast packet is sent. 1389 */ 1390 if (addr.s_addr == INADDR_ANY) { 1391 imo->imo_ifidx = 0; 1392 break; 1393 } 1394 /* 1395 * The selected interface is identified by its local 1396 * IP address. Find the interface and confirm that 1397 * it supports multicasting. 1398 */ 1399 memset(&sin, 0, sizeof(sin)); 1400 sin.sin_len = sizeof(sin); 1401 sin.sin_family = AF_INET; 1402 sin.sin_addr = addr; 1403 ia = ifatoia(ifa_ifwithaddr(sintosa(&sin), rtableid)); 1404 if (ia == NULL || 1405 (ia->ia_ifp->if_flags & IFF_MULTICAST) == 0) { 1406 error = EADDRNOTAVAIL; 1407 break; 1408 } 1409 imo->imo_ifidx = ia->ia_ifp->if_index; 1410 break; 1411 1412 case IP_MULTICAST_TTL: 1413 /* 1414 * Set the IP time-to-live for outgoing multicast packets. 1415 */ 1416 if (m == NULL || m->m_len != 1) { 1417 error = EINVAL; 1418 break; 1419 } 1420 imo->imo_ttl = *(mtod(m, u_char *)); 1421 break; 1422 1423 case IP_MULTICAST_LOOP: 1424 /* 1425 * Set the loopback flag for outgoing multicast packets. 1426 * Must be zero or one. 1427 */ 1428 if (m == NULL || m->m_len != 1 || 1429 (loop = *(mtod(m, u_char *))) > 1) { 1430 error = EINVAL; 1431 break; 1432 } 1433 imo->imo_loop = loop; 1434 break; 1435 1436 case IP_ADD_MEMBERSHIP: 1437 /* 1438 * Add a multicast group membership. 1439 * Group must be a valid IP multicast address. 1440 */ 1441 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 1442 error = EINVAL; 1443 break; 1444 } 1445 mreq = mtod(m, struct ip_mreq *); 1446 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) { 1447 error = EINVAL; 1448 break; 1449 } 1450 /* 1451 * If no interface address was provided, use the interface of 1452 * the route to the given multicast address. 1453 */ 1454 if (mreq->imr_interface.s_addr == INADDR_ANY) { 1455 memset(&sin, 0, sizeof(sin)); 1456 sin.sin_len = sizeof(sin); 1457 sin.sin_family = AF_INET; 1458 sin.sin_addr = mreq->imr_multiaddr; 1459 rt = rtalloc(sintosa(&sin), RT_RESOLVE, rtableid); 1460 if (!rtisvalid(rt)) { 1461 rtfree(rt); 1462 error = EADDRNOTAVAIL; 1463 break; 1464 } 1465 } else { 1466 memset(&sin, 0, sizeof(sin)); 1467 sin.sin_len = sizeof(sin); 1468 sin.sin_family = AF_INET; 1469 sin.sin_addr = mreq->imr_interface; 1470 rt = rtalloc(sintosa(&sin), 0, rtableid); 1471 if (!rtisvalid(rt) || !ISSET(rt->rt_flags, RTF_LOCAL)) { 1472 rtfree(rt); 1473 error = EADDRNOTAVAIL; 1474 break; 1475 } 1476 } 1477 ifp = if_get(rt->rt_ifidx); 1478 rtfree(rt); 1479 1480 /* 1481 * See if we found an interface, and confirm that it 1482 * supports multicast. 1483 */ 1484 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1485 error = EADDRNOTAVAIL; 1486 if_put(ifp); 1487 break; 1488 } 1489 /* 1490 * See if the membership already exists or if all the 1491 * membership slots are full. 1492 */ 1493 for (i = 0; i < imo->imo_num_memberships; ++i) { 1494 if (imo->imo_membership[i]->inm_ifidx 1495 == ifp->if_index && 1496 imo->imo_membership[i]->inm_addr.s_addr 1497 == mreq->imr_multiaddr.s_addr) 1498 break; 1499 } 1500 if (i < imo->imo_num_memberships) { 1501 error = EADDRINUSE; 1502 if_put(ifp); 1503 break; 1504 } 1505 if (imo->imo_num_memberships == imo->imo_max_memberships) { 1506 struct in_multi **nmships, **omships; 1507 size_t newmax; 1508 /* 1509 * Resize the vector to next power-of-two minus 1. If the 1510 * size would exceed the maximum then we know we've really 1511 * run out of entries. Otherwise, we reallocate the vector. 1512 */ 1513 nmships = NULL; 1514 omships = imo->imo_membership; 1515 newmax = ((imo->imo_max_memberships + 1) * 2) - 1; 1516 if (newmax <= IP_MAX_MEMBERSHIPS) { 1517 nmships = mallocarray(newmax, sizeof(*nmships), 1518 M_IPMOPTS, M_NOWAIT|M_ZERO); 1519 if (nmships != NULL) { 1520 memcpy(nmships, omships, 1521 sizeof(*omships) * 1522 imo->imo_max_memberships); 1523 free(omships, M_IPMOPTS, 1524 sizeof(*omships) * 1525 imo->imo_max_memberships); 1526 imo->imo_membership = nmships; 1527 imo->imo_max_memberships = newmax; 1528 } 1529 } 1530 if (nmships == NULL) { 1531 error = ENOBUFS; 1532 if_put(ifp); 1533 break; 1534 } 1535 } 1536 /* 1537 * Everything looks good; add a new record to the multicast 1538 * address list for the given interface. 1539 */ 1540 if ((imo->imo_membership[i] = 1541 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) { 1542 error = ENOBUFS; 1543 if_put(ifp); 1544 break; 1545 } 1546 ++imo->imo_num_memberships; 1547 if_put(ifp); 1548 break; 1549 1550 case IP_DROP_MEMBERSHIP: 1551 /* 1552 * Drop a multicast group membership. 1553 * Group must be a valid IP multicast address. 1554 */ 1555 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 1556 error = EINVAL; 1557 break; 1558 } 1559 mreq = mtod(m, struct ip_mreq *); 1560 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) { 1561 error = EINVAL; 1562 break; 1563 } 1564 /* 1565 * If an interface address was specified, get a pointer 1566 * to its ifnet structure. 1567 */ 1568 if (mreq->imr_interface.s_addr == INADDR_ANY) 1569 ifp = NULL; 1570 else { 1571 memset(&sin, 0, sizeof(sin)); 1572 sin.sin_len = sizeof(sin); 1573 sin.sin_family = AF_INET; 1574 sin.sin_addr = mreq->imr_interface; 1575 ia = ifatoia(ifa_ifwithaddr(sintosa(&sin), rtableid)); 1576 if (ia == NULL) { 1577 error = EADDRNOTAVAIL; 1578 break; 1579 } 1580 ifp = ia->ia_ifp; 1581 } 1582 /* 1583 * Find the membership in the membership array. 1584 */ 1585 for (i = 0; i < imo->imo_num_memberships; ++i) { 1586 if ((ifp == NULL || 1587 imo->imo_membership[i]->inm_ifidx == 1588 ifp->if_index) && 1589 imo->imo_membership[i]->inm_addr.s_addr == 1590 mreq->imr_multiaddr.s_addr) 1591 break; 1592 } 1593 if (i == imo->imo_num_memberships) { 1594 error = EADDRNOTAVAIL; 1595 break; 1596 } 1597 /* 1598 * Give up the multicast address record to which the 1599 * membership points. 1600 */ 1601 in_delmulti(imo->imo_membership[i]); 1602 /* 1603 * Remove the gap in the membership array. 1604 */ 1605 for (++i; i < imo->imo_num_memberships; ++i) 1606 imo->imo_membership[i-1] = imo->imo_membership[i]; 1607 --imo->imo_num_memberships; 1608 break; 1609 1610 default: 1611 error = EOPNOTSUPP; 1612 break; 1613 } 1614 1615 /* 1616 * If all options have default values, no need to keep the data. 1617 */ 1618 if (imo->imo_ifidx == 0 && 1619 imo->imo_ttl == IP_DEFAULT_MULTICAST_TTL && 1620 imo->imo_loop == IP_DEFAULT_MULTICAST_LOOP && 1621 imo->imo_num_memberships == 0) { 1622 free(imo->imo_membership , M_IPMOPTS, 1623 imo->imo_max_memberships * sizeof(struct in_multi *)); 1624 free(*imop, M_IPMOPTS, sizeof(**imop)); 1625 *imop = NULL; 1626 } 1627 1628 return (error); 1629 } 1630 1631 /* 1632 * Return the IP multicast options in response to user getsockopt(). 1633 */ 1634 int 1635 ip_getmoptions(int optname, struct ip_moptions *imo, struct mbuf *m) 1636 { 1637 u_char *ttl; 1638 u_char *loop; 1639 struct in_addr *addr; 1640 struct in_ifaddr *ia; 1641 struct ifnet *ifp; 1642 1643 switch (optname) { 1644 1645 case IP_MULTICAST_IF: 1646 addr = mtod(m, struct in_addr *); 1647 m->m_len = sizeof(struct in_addr); 1648 if (imo == NULL || (ifp = if_get(imo->imo_ifidx)) == NULL) 1649 addr->s_addr = INADDR_ANY; 1650 else { 1651 IFP_TO_IA(ifp, ia); 1652 if_put(ifp); 1653 addr->s_addr = (ia == NULL) ? INADDR_ANY 1654 : ia->ia_addr.sin_addr.s_addr; 1655 } 1656 return (0); 1657 1658 case IP_MULTICAST_TTL: 1659 ttl = mtod(m, u_char *); 1660 m->m_len = 1; 1661 *ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL 1662 : imo->imo_ttl; 1663 return (0); 1664 1665 case IP_MULTICAST_LOOP: 1666 loop = mtod(m, u_char *); 1667 m->m_len = 1; 1668 *loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP 1669 : imo->imo_loop; 1670 return (0); 1671 1672 default: 1673 return (EOPNOTSUPP); 1674 } 1675 } 1676 1677 /* 1678 * Discard the IP multicast options. 1679 */ 1680 void 1681 ip_freemoptions(struct ip_moptions *imo) 1682 { 1683 int i; 1684 1685 if (imo != NULL) { 1686 for (i = 0; i < imo->imo_num_memberships; ++i) 1687 in_delmulti(imo->imo_membership[i]); 1688 free(imo->imo_membership, M_IPMOPTS, 1689 imo->imo_max_memberships * sizeof(struct in_multi *)); 1690 free(imo, M_IPMOPTS, sizeof(*imo)); 1691 } 1692 } 1693 1694 /* 1695 * Routine called from ip_output() to loop back a copy of an IP multicast 1696 * packet to the input queue of a specified interface. 1697 */ 1698 void 1699 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst) 1700 { 1701 struct ip *ip; 1702 struct mbuf *copym; 1703 1704 copym = m_dup_pkt(m, max_linkhdr, M_DONTWAIT); 1705 if (copym != NULL) { 1706 /* 1707 * We don't bother to fragment if the IP length is greater 1708 * than the interface's MTU. Can this possibly matter? 1709 */ 1710 ip = mtod(copym, struct ip *); 1711 ip->ip_sum = 0; 1712 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2); 1713 if_input_local(ifp, copym, dst->sin_family); 1714 } 1715 } 1716 1717 /* 1718 * Compute significant parts of the IPv4 checksum pseudo-header 1719 * for use in a delayed TCP/UDP checksum calculation. 1720 */ 1721 static __inline u_int16_t __attribute__((__unused__)) 1722 in_cksum_phdr(u_int32_t src, u_int32_t dst, u_int32_t lenproto) 1723 { 1724 u_int32_t sum; 1725 1726 sum = lenproto + 1727 (u_int16_t)(src >> 16) + 1728 (u_int16_t)(src /*& 0xffff*/) + 1729 (u_int16_t)(dst >> 16) + 1730 (u_int16_t)(dst /*& 0xffff*/); 1731 1732 sum = (u_int16_t)(sum >> 16) + (u_int16_t)(sum /*& 0xffff*/); 1733 1734 if (sum > 0xffff) 1735 sum -= 0xffff; 1736 1737 return (sum); 1738 } 1739 1740 /* 1741 * Process a delayed payload checksum calculation. 1742 */ 1743 void 1744 in_delayed_cksum(struct mbuf *m) 1745 { 1746 struct ip *ip; 1747 u_int16_t csum, offset; 1748 1749 ip = mtod(m, struct ip *); 1750 offset = ip->ip_hl << 2; 1751 csum = in4_cksum(m, 0, offset, m->m_pkthdr.len - offset); 1752 if (csum == 0 && ip->ip_p == IPPROTO_UDP) 1753 csum = 0xffff; 1754 1755 switch (ip->ip_p) { 1756 case IPPROTO_TCP: 1757 offset += offsetof(struct tcphdr, th_sum); 1758 break; 1759 1760 case IPPROTO_UDP: 1761 offset += offsetof(struct udphdr, uh_sum); 1762 break; 1763 1764 case IPPROTO_ICMP: 1765 offset += offsetof(struct icmp, icmp_cksum); 1766 break; 1767 1768 default: 1769 return; 1770 } 1771 1772 if ((offset + sizeof(u_int16_t)) > m->m_len) 1773 m_copyback(m, offset, sizeof(csum), &csum, M_NOWAIT); 1774 else 1775 *(u_int16_t *)(mtod(m, caddr_t) + offset) = csum; 1776 } 1777 1778 void 1779 in_proto_cksum_out(struct mbuf *m, struct ifnet *ifp) 1780 { 1781 struct ip *ip = mtod(m, struct ip *); 1782 1783 /* some hw and in_delayed_cksum need the pseudo header cksum */ 1784 if (m->m_pkthdr.csum_flags & 1785 (M_TCP_CSUM_OUT|M_UDP_CSUM_OUT|M_ICMP_CSUM_OUT)) { 1786 u_int16_t csum = 0, offset; 1787 1788 offset = ip->ip_hl << 2; 1789 if (m->m_pkthdr.csum_flags & (M_TCP_CSUM_OUT|M_UDP_CSUM_OUT)) 1790 csum = in_cksum_phdr(ip->ip_src.s_addr, 1791 ip->ip_dst.s_addr, htonl(ntohs(ip->ip_len) - 1792 offset + ip->ip_p)); 1793 if (ip->ip_p == IPPROTO_TCP) 1794 offset += offsetof(struct tcphdr, th_sum); 1795 else if (ip->ip_p == IPPROTO_UDP) 1796 offset += offsetof(struct udphdr, uh_sum); 1797 else if (ip->ip_p == IPPROTO_ICMP) 1798 offset += offsetof(struct icmp, icmp_cksum); 1799 if ((offset + sizeof(u_int16_t)) > m->m_len) 1800 m_copyback(m, offset, sizeof(csum), &csum, M_NOWAIT); 1801 else 1802 *(u_int16_t *)(mtod(m, caddr_t) + offset) = csum; 1803 } 1804 1805 if (m->m_pkthdr.csum_flags & M_TCP_CSUM_OUT) { 1806 if (!ifp || !(ifp->if_capabilities & IFCAP_CSUM_TCPv4) || 1807 ip->ip_hl != 5 || ifp->if_bridgeidx != 0) { 1808 tcpstat_inc(tcps_outswcsum); 1809 in_delayed_cksum(m); 1810 m->m_pkthdr.csum_flags &= ~M_TCP_CSUM_OUT; /* Clear */ 1811 } 1812 } else if (m->m_pkthdr.csum_flags & M_UDP_CSUM_OUT) { 1813 if (!ifp || !(ifp->if_capabilities & IFCAP_CSUM_UDPv4) || 1814 ip->ip_hl != 5 || ifp->if_bridgeidx != 0) { 1815 udpstat_inc(udps_outswcsum); 1816 in_delayed_cksum(m); 1817 m->m_pkthdr.csum_flags &= ~M_UDP_CSUM_OUT; /* Clear */ 1818 } 1819 } else if (m->m_pkthdr.csum_flags & M_ICMP_CSUM_OUT) { 1820 in_delayed_cksum(m); 1821 m->m_pkthdr.csum_flags &= ~M_ICMP_CSUM_OUT; /* Clear */ 1822 } 1823 } 1824