1 /* $OpenBSD: ip_output.c,v 1.229 2012/04/13 09:38:32 deraadt 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_enc.h> 48 #include <net/route.h> 49 50 #include <netinet/in.h> 51 #include <netinet/in_systm.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 74 extern u_int8_t get_sa_require(struct inpcb *); 75 76 extern int ipsec_auth_default_level; 77 extern int ipsec_esp_trans_default_level; 78 extern int ipsec_esp_network_default_level; 79 extern int ipsec_ipcomp_default_level; 80 extern int ipforwarding; 81 #endif /* IPSEC */ 82 83 #ifdef MROUTING 84 extern int ipmforwarding; 85 #endif 86 87 struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *); 88 void ip_mloopback(struct ifnet *, struct mbuf *, struct sockaddr_in *); 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, ...) 98 { 99 struct ip *ip; 100 struct ifnet *ifp; 101 struct mbuf *m = m0; 102 int hlen = sizeof (struct ip); 103 int len, error = 0; 104 struct route iproute; 105 struct sockaddr_in *dst; 106 struct in_ifaddr *ia; 107 struct mbuf *opt; 108 struct route *ro; 109 int flags; 110 struct ip_moptions *imo; 111 va_list ap; 112 u_int8_t sproto = 0, donerouting = 0; 113 u_long mtu; 114 #ifdef IPSEC 115 u_int32_t icmp_mtu = 0; 116 union sockaddr_union sdst; 117 u_int32_t sspi; 118 struct m_tag *mtag; 119 struct tdb_ident *tdbi; 120 121 struct inpcb *inp; 122 struct tdb *tdb; 123 int s; 124 #if NPF > 0 125 struct ifnet *encif; 126 #endif 127 #endif /* IPSEC */ 128 129 va_start(ap, m0); 130 opt = va_arg(ap, struct mbuf *); 131 ro = va_arg(ap, struct route *); 132 flags = va_arg(ap, int); 133 imo = va_arg(ap, struct ip_moptions *); 134 #ifdef IPSEC 135 inp = va_arg(ap, struct inpcb *); 136 if (inp && (inp->inp_flags & INP_IPV6) != 0) 137 panic("ip_output: IPv6 pcb is passed"); 138 #endif /* IPSEC */ 139 va_end(ap); 140 141 #ifdef DIAGNOSTIC 142 if ((m->m_flags & M_PKTHDR) == 0) 143 panic("ip_output no HDR"); 144 #endif 145 if (opt) { 146 m = ip_insertoptions(m, opt, &len); 147 hlen = len; 148 } 149 150 ip = mtod(m, struct ip *); 151 152 /* 153 * Fill in IP header. 154 */ 155 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 156 ip->ip_v = IPVERSION; 157 ip->ip_off &= htons(IP_DF); 158 ip->ip_id = htons(ip_randomid()); 159 ip->ip_hl = hlen >> 2; 160 ipstat.ips_localout++; 161 } else { 162 hlen = ip->ip_hl << 2; 163 } 164 165 /* 166 * We should not send traffic to 0/8 say both Stevens and RFCs 167 * 5735 section 3 and 1122 sections 3.2.1.3 and 3.3.6. 168 */ 169 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == 0) { 170 error = ENETUNREACH; 171 goto bad; 172 } 173 174 /* 175 * If we're missing the IP source address, do a route lookup. We'll 176 * remember this result, in case we don't need to do any IPsec 177 * processing on the packet. We need the source address so we can 178 * do an SPD lookup in IPsec; for most packets, the source address 179 * is set at a higher level protocol. ICMPs and other packets 180 * though (e.g., traceroute) have a source address of zeroes. 181 */ 182 if (ip->ip_src.s_addr == INADDR_ANY) { 183 if (flags & IP_ROUTETOETHER) { 184 error = EINVAL; 185 goto bad; 186 } 187 donerouting = 1; 188 189 if (ro == 0) { 190 ro = &iproute; 191 bzero((caddr_t)ro, sizeof (*ro)); 192 } 193 194 dst = satosin(&ro->ro_dst); 195 196 /* 197 * If there is a cached route, check that it is to the same 198 * destination and is still up. If not, free it and try again. 199 */ 200 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 201 dst->sin_addr.s_addr != ip->ip_dst.s_addr || 202 ro->ro_tableid != m->m_pkthdr.rdomain)) { 203 RTFREE(ro->ro_rt); 204 ro->ro_rt = (struct rtentry *)0; 205 } 206 207 if (ro->ro_rt == 0) { 208 dst->sin_family = AF_INET; 209 dst->sin_len = sizeof(*dst); 210 dst->sin_addr = ip->ip_dst; 211 ro->ro_tableid = m->m_pkthdr.rdomain; 212 } 213 214 /* 215 * If routing to interface only, short-circuit routing lookup. 216 */ 217 if (flags & IP_ROUTETOIF) { 218 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst), 219 m->m_pkthdr.rdomain))) == 0 && 220 (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 221 m->m_pkthdr.rdomain))) == 0) { 222 ipstat.ips_noroute++; 223 error = ENETUNREACH; 224 goto bad; 225 } 226 227 ifp = ia->ia_ifp; 228 mtu = ifp->if_mtu; 229 ip->ip_ttl = 1; 230 } else if ((IN_MULTICAST(ip->ip_dst.s_addr) || 231 (ip->ip_dst.s_addr == INADDR_BROADCAST)) && 232 imo != NULL && imo->imo_multicast_ifp != NULL) { 233 ifp = imo->imo_multicast_ifp; 234 mtu = ifp->if_mtu; 235 IFP_TO_IA(ifp, ia); 236 } else { 237 if (ro->ro_rt == 0) 238 rtalloc_mpath(ro, NULL); 239 240 if (ro->ro_rt == 0) { 241 ipstat.ips_noroute++; 242 error = EHOSTUNREACH; 243 goto bad; 244 } 245 246 ia = ifatoia(ro->ro_rt->rt_ifa); 247 ifp = ro->ro_rt->rt_ifp; 248 if ((mtu = ro->ro_rt->rt_rmx.rmx_mtu) == 0) 249 mtu = ifp->if_mtu; 250 ro->ro_rt->rt_use++; 251 252 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 253 dst = satosin(ro->ro_rt->rt_gateway); 254 } 255 256 /* Set the source IP address */ 257 if (!IN_MULTICAST(ip->ip_dst.s_addr)) 258 ip->ip_src = ia->ia_addr.sin_addr; 259 } 260 261 #if NPF > 0 262 reroute: 263 #endif 264 265 #ifdef IPSEC 266 if (!ipsec_in_use && inp == NULL) 267 goto done_spd; 268 269 /* 270 * splnet is chosen over spltdb because we are not allowed to 271 * lower the level, and udp_output calls us in splnet(). 272 */ 273 s = splnet(); 274 275 /* Do we have any pending SAs to apply ? */ 276 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL); 277 if (mtag != NULL) { 278 #ifdef DIAGNOSTIC 279 if (mtag->m_tag_len != sizeof (struct tdb_ident)) 280 panic("ip_output: tag of length %d (should be %d", 281 mtag->m_tag_len, sizeof (struct tdb_ident)); 282 #endif 283 tdbi = (struct tdb_ident *)(mtag + 1); 284 tdb = gettdb(tdbi->rdomain, 285 tdbi->spi, &tdbi->dst, tdbi->proto); 286 if (tdb == NULL) 287 error = -EINVAL; 288 m_tag_delete(m, mtag); 289 } 290 else 291 tdb = ipsp_spd_lookup(m, AF_INET, hlen, &error, 292 IPSP_DIRECTION_OUT, NULL, inp); 293 294 if (tdb == NULL) { 295 splx(s); 296 297 if (error == 0) { 298 /* 299 * No IPsec processing required, we'll just send the 300 * packet out. 301 */ 302 sproto = 0; 303 304 /* Fall through to routing/multicast handling */ 305 } else { 306 /* 307 * -EINVAL is used to indicate that the packet should 308 * be silently dropped, typically because we've asked 309 * key management for an SA. 310 */ 311 if (error == -EINVAL) /* Should silently drop packet */ 312 error = 0; 313 314 m_freem(m); 315 goto done; 316 } 317 } else { 318 /* Loop detection */ 319 for (mtag = m_tag_first(m); mtag != NULL; 320 mtag = m_tag_next(m, mtag)) { 321 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE && 322 mtag->m_tag_id != 323 PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED) 324 continue; 325 tdbi = (struct tdb_ident *)(mtag + 1); 326 if (tdbi->spi == tdb->tdb_spi && 327 tdbi->proto == tdb->tdb_sproto && 328 tdbi->rdomain == tdb->tdb_rdomain && 329 !bcmp(&tdbi->dst, &tdb->tdb_dst, 330 sizeof(union sockaddr_union))) { 331 splx(s); 332 sproto = 0; /* mark as no-IPsec-needed */ 333 goto done_spd; 334 } 335 } 336 337 /* We need to do IPsec */ 338 bcopy(&tdb->tdb_dst, &sdst, sizeof(sdst)); 339 sspi = tdb->tdb_spi; 340 sproto = tdb->tdb_sproto; 341 splx(s); 342 343 /* 344 * If it needs TCP/UDP hardware-checksumming, do the 345 * computation now. 346 */ 347 in_proto_cksum_out(m, NULL); 348 349 /* If it's not a multicast packet, try to fast-path */ 350 if (!IN_MULTICAST(ip->ip_dst.s_addr)) { 351 goto sendit; 352 } 353 } 354 355 /* Fall through to the routing/multicast handling code */ 356 done_spd: 357 #endif /* IPSEC */ 358 359 if (flags & IP_ROUTETOETHER) { 360 dst = satosin(&ro->ro_dst); 361 ifp = ro->ro_rt->rt_ifp; 362 mtu = ifp->if_mtu; 363 ro->ro_rt = NULL; 364 } else if (donerouting == 0) { 365 if (ro == 0) { 366 ro = &iproute; 367 bzero((caddr_t)ro, sizeof (*ro)); 368 } 369 370 dst = satosin(&ro->ro_dst); 371 372 /* 373 * If there is a cached route, check that it is to the same 374 * destination and is still up. If not, free it and try again. 375 */ 376 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 377 dst->sin_addr.s_addr != ip->ip_dst.s_addr || 378 ro->ro_tableid != m->m_pkthdr.rdomain)) { 379 RTFREE(ro->ro_rt); 380 ro->ro_rt = (struct rtentry *)0; 381 } 382 383 if (ro->ro_rt == 0) { 384 dst->sin_family = AF_INET; 385 dst->sin_len = sizeof(*dst); 386 dst->sin_addr = ip->ip_dst; 387 ro->ro_tableid = m->m_pkthdr.rdomain; 388 } 389 390 /* 391 * If routing to interface only, short-circuit routing lookup. 392 */ 393 if (flags & IP_ROUTETOIF) { 394 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst), 395 m->m_pkthdr.rdomain))) == 0 && 396 (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 397 m->m_pkthdr.rdomain))) == 0) { 398 ipstat.ips_noroute++; 399 error = ENETUNREACH; 400 goto bad; 401 } 402 403 ifp = ia->ia_ifp; 404 mtu = ifp->if_mtu; 405 ip->ip_ttl = 1; 406 } else if ((IN_MULTICAST(ip->ip_dst.s_addr) || 407 (ip->ip_dst.s_addr == INADDR_BROADCAST)) && 408 imo != NULL && imo->imo_multicast_ifp != NULL) { 409 ifp = imo->imo_multicast_ifp; 410 mtu = ifp->if_mtu; 411 IFP_TO_IA(ifp, ia); 412 } else { 413 if (ro->ro_rt == 0) 414 rtalloc_mpath(ro, &ip->ip_src.s_addr); 415 416 if (ro->ro_rt == 0) { 417 ipstat.ips_noroute++; 418 error = EHOSTUNREACH; 419 goto bad; 420 } 421 422 ia = ifatoia(ro->ro_rt->rt_ifa); 423 ifp = ro->ro_rt->rt_ifp; 424 if ((mtu = ro->ro_rt->rt_rmx.rmx_mtu) == 0) 425 mtu = ifp->if_mtu; 426 ro->ro_rt->rt_use++; 427 428 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 429 dst = satosin(ro->ro_rt->rt_gateway); 430 } 431 432 /* Set the source IP address */ 433 if (ip->ip_src.s_addr == INADDR_ANY) 434 ip->ip_src = ia->ia_addr.sin_addr; 435 } 436 437 if (IN_MULTICAST(ip->ip_dst.s_addr) || 438 (ip->ip_dst.s_addr == INADDR_BROADCAST)) { 439 struct in_multi *inm; 440 441 m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ? 442 M_BCAST : M_MCAST; 443 444 /* 445 * IP destination address is multicast. Make sure "dst" 446 * still points to the address in "ro". (It may have been 447 * changed to point to a gateway address, above.) 448 */ 449 dst = satosin(&ro->ro_dst); 450 451 /* 452 * See if the caller provided any multicast options 453 */ 454 if (imo != NULL) 455 ip->ip_ttl = imo->imo_multicast_ttl; 456 else 457 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 458 459 /* 460 * if we don't know the outgoing ifp yet, we can't generate 461 * output 462 */ 463 if (!ifp) { 464 ipstat.ips_noroute++; 465 error = EHOSTUNREACH; 466 goto bad; 467 } 468 469 /* 470 * Confirm that the outgoing interface supports multicast, 471 * but only if the packet actually is going out on that 472 * interface (i.e., no IPsec is applied). 473 */ 474 if ((((m->m_flags & M_MCAST) && 475 (ifp->if_flags & IFF_MULTICAST) == 0) || 476 ((m->m_flags & M_BCAST) && 477 (ifp->if_flags & IFF_BROADCAST) == 0)) && (sproto == 0)) { 478 ipstat.ips_noroute++; 479 error = ENETUNREACH; 480 goto bad; 481 } 482 483 /* 484 * If source address not specified yet, use address 485 * of outgoing interface. 486 */ 487 if (ip->ip_src.s_addr == INADDR_ANY) { 488 struct in_ifaddr *ia; 489 490 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) 491 if (ia->ia_ifp == ifp) { 492 ip->ip_src = ia->ia_addr.sin_addr; 493 break; 494 } 495 } 496 497 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 498 if (inm != NULL && 499 (imo == NULL || imo->imo_multicast_loop)) { 500 /* 501 * If we belong to the destination multicast group 502 * on the outgoing interface, and the caller did not 503 * forbid loopback, loop back a copy. 504 * Can't defer TCP/UDP checksumming, do the 505 * computation now. 506 */ 507 in_proto_cksum_out(m, NULL); 508 ip_mloopback(ifp, m, dst); 509 } 510 #ifdef MROUTING 511 else { 512 /* 513 * If we are acting as a multicast router, perform 514 * multicast forwarding as if the packet had just 515 * arrived on the interface to which we are about 516 * to send. The multicast forwarding function 517 * recursively calls this function, using the 518 * IP_FORWARDING flag to prevent infinite recursion. 519 * 520 * Multicasts that are looped back by ip_mloopback(), 521 * above, will be forwarded by the ip_input() routine, 522 * if necessary. 523 */ 524 extern struct socket *ip_mrouter; 525 526 if (ipmforwarding && ip_mrouter && 527 (flags & IP_FORWARDING) == 0) { 528 if (ip_mforward(m, ifp) != 0) { 529 m_freem(m); 530 goto done; 531 } 532 } 533 } 534 #endif 535 /* 536 * Multicasts with a time-to-live of zero may be looped- 537 * back, above, but must not be transmitted on a network. 538 * Also, multicasts addressed to the loopback interface 539 * are not sent -- the above call to ip_mloopback() will 540 * loop back a copy if this host actually belongs to the 541 * destination group on the loopback interface. 542 */ 543 if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) { 544 m_freem(m); 545 goto done; 546 } 547 548 goto sendit; 549 } 550 551 /* 552 * Look for broadcast address and verify user is allowed to send 553 * such a packet; if the packet is going in an IPsec tunnel, skip 554 * this check. 555 */ 556 if ((sproto == 0) && (in_broadcast(dst->sin_addr, ifp, 557 m->m_pkthdr.rdomain))) { 558 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 559 error = EADDRNOTAVAIL; 560 goto bad; 561 } 562 if ((flags & IP_ALLOWBROADCAST) == 0) { 563 error = EACCES; 564 goto bad; 565 } 566 567 /* Don't allow broadcast messages to be fragmented */ 568 if (ntohs(ip->ip_len) > ifp->if_mtu) { 569 error = EMSGSIZE; 570 goto bad; 571 } 572 m->m_flags |= M_BCAST; 573 } else 574 m->m_flags &= ~M_BCAST; 575 576 sendit: 577 /* 578 * If we're doing Path MTU discovery, we need to set DF unless 579 * the route's MTU is locked. 580 */ 581 if ((flags & IP_MTUDISC) && ro && ro->ro_rt && 582 (ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0) 583 ip->ip_off |= htons(IP_DF); 584 585 #ifdef IPSEC 586 /* 587 * Check if the packet needs encapsulation. 588 */ 589 if (sproto != 0) { 590 s = splnet(); 591 592 tdb = gettdb(rtable_l2(m->m_pkthdr.rdomain), 593 sspi, &sdst, sproto); 594 if (tdb == NULL) { 595 DPRINTF(("ip_output: unknown TDB")); 596 error = EHOSTUNREACH; 597 splx(s); 598 m_freem(m); 599 goto done; 600 } 601 602 /* 603 * Packet filter 604 */ 605 #if NPF > 0 606 if ((encif = enc_getif(tdb->tdb_rdomain, 607 tdb->tdb_tap)) == NULL || 608 pf_test(AF_INET, PF_OUT, encif, &m, NULL) != PF_PASS) { 609 error = EACCES; 610 splx(s); 611 m_freem(m); 612 goto done; 613 } 614 if (m == NULL) { 615 splx(s); 616 goto done; 617 } 618 ip = mtod(m, struct ip *); 619 hlen = ip->ip_hl << 2; 620 /* 621 * PF_TAG_REROUTE handling or not... 622 * Packet is entering IPsec so the routing is 623 * already overruled by the IPsec policy. 624 * Until now the change was not reconsidered. 625 * What's the behaviour? 626 */ 627 #endif 628 629 /* Check if we are allowed to fragment */ 630 if (ip_mtudisc && (ip->ip_off & htons(IP_DF)) && tdb->tdb_mtu && 631 ntohs(ip->ip_len) > tdb->tdb_mtu && 632 tdb->tdb_mtutimeout > time_second) { 633 struct rtentry *rt = NULL; 634 int rt_mtucloned = 0; 635 int transportmode = 0; 636 637 transportmode = (tdb->tdb_dst.sa.sa_family == AF_INET) && 638 (tdb->tdb_dst.sin.sin_addr.s_addr == 639 ip->ip_dst.s_addr); 640 icmp_mtu = tdb->tdb_mtu; 641 splx(s); 642 643 /* Find a host route to store the mtu in */ 644 if (ro != NULL) 645 rt = ro->ro_rt; 646 /* but don't add a PMTU route for transport mode SAs */ 647 if (transportmode) 648 rt = NULL; 649 else if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0) { 650 struct sockaddr_in dst = { 651 sizeof(struct sockaddr_in), AF_INET}; 652 dst.sin_addr = ip->ip_dst; 653 rt = icmp_mtudisc_clone((struct sockaddr *)&dst, 654 m->m_pkthdr.rdomain); 655 rt_mtucloned = 1; 656 } 657 DPRINTF(("ip_output: spi %08x mtu %d rt %p cloned %d\n", 658 ntohl(tdb->tdb_spi), icmp_mtu, rt, rt_mtucloned)); 659 if (rt != NULL) { 660 rt->rt_rmx.rmx_mtu = icmp_mtu; 661 if (ro && ro->ro_rt != NULL) { 662 RTFREE(ro->ro_rt); 663 ro->ro_rt = rtalloc1(&ro->ro_dst, RT_REPORT, 664 m->m_pkthdr.rdomain); 665 } 666 if (rt_mtucloned) 667 rtfree(rt); 668 } 669 error = EMSGSIZE; 670 goto bad; 671 } 672 673 /* 674 * Clear these -- they'll be set in the recursive invocation 675 * as needed. 676 */ 677 m->m_flags &= ~(M_MCAST | M_BCAST); 678 679 /* Callee frees mbuf */ 680 error = ipsp_process_packet(m, tdb, AF_INET, 0); 681 splx(s); 682 return error; /* Nothing more to be done */ 683 } 684 685 /* 686 * If we got here and IPsec crypto processing didn't happen, drop it. 687 */ 688 if (ipsec_in_use && (mtag = m_tag_find(m, 689 PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL)) != NULL) { 690 /* Notify IPsec to do its own crypto. */ 691 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1)); 692 m_freem(m); 693 error = EHOSTUNREACH; 694 goto done; 695 } 696 #endif /* IPSEC */ 697 698 in_proto_cksum_out(m, ifp); 699 700 /* 701 * Packet filter 702 */ 703 #if NPF > 0 704 if (pf_test(AF_INET, PF_OUT, ifp, &m, NULL) != PF_PASS) { 705 error = EHOSTUNREACH; 706 m_freem(m); 707 goto done; 708 } 709 if (m == NULL) 710 goto done; 711 ip = mtod(m, struct ip *); 712 hlen = ip->ip_hl << 2; 713 if ((m->m_pkthdr.pf.flags & (PF_TAG_REROUTE | PF_TAG_GENERATED)) == 714 (PF_TAG_REROUTE | PF_TAG_GENERATED)) 715 /* already rerun the route lookup, go on */ 716 m->m_pkthdr.pf.flags &= ~(PF_TAG_GENERATED | PF_TAG_REROUTE); 717 else if (m->m_pkthdr.pf.flags & PF_TAG_REROUTE) { 718 /* tag as generated to skip over pf_test on rerun */ 719 m->m_pkthdr.pf.flags |= PF_TAG_GENERATED; 720 ro = NULL; 721 donerouting = 0; 722 goto reroute; 723 } 724 #endif 725 726 #ifdef IPSEC 727 if (ipsec_in_use && (flags & IP_FORWARDING) && (ipforwarding == 2) && 728 (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) == NULL)) { 729 error = EHOSTUNREACH; 730 m_freem(m); 731 goto done; 732 } 733 #endif 734 735 /* 736 * If small enough for interface, can just send directly. 737 */ 738 if (ntohs(ip->ip_len) <= mtu) { 739 ip->ip_sum = 0; 740 if ((ifp->if_capabilities & IFCAP_CSUM_IPv4) && 741 (ifp->if_bridge == NULL)) { 742 m->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT; 743 ipstat.ips_outhwcsum++; 744 } else 745 ip->ip_sum = in_cksum(m, hlen); 746 /* Update relevant hardware checksum stats for TCP/UDP */ 747 if (m->m_pkthdr.csum_flags & M_TCP_CSUM_OUT) 748 tcpstat.tcps_outhwcsum++; 749 else if (m->m_pkthdr.csum_flags & M_UDP_CSUM_OUT) 750 udpstat.udps_outhwcsum++; 751 error = (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt); 752 goto done; 753 } 754 755 /* 756 * Too large for interface; fragment if possible. 757 * Must be able to put at least 8 bytes per fragment. 758 */ 759 if (ip->ip_off & htons(IP_DF)) { 760 #ifdef IPSEC 761 icmp_mtu = ifp->if_mtu; 762 #endif 763 error = EMSGSIZE; 764 /* 765 * This case can happen if the user changed the MTU 766 * of an interface after enabling IP on it. Because 767 * most netifs don't keep track of routes pointing to 768 * them, there is no way for one to update all its 769 * routes when the MTU is changed. 770 */ 771 if (ro->ro_rt != NULL && 772 (ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) && 773 !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) && 774 (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 775 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 776 } 777 ipstat.ips_cantfrag++; 778 goto bad; 779 } 780 781 error = ip_fragment(m, ifp, mtu); 782 if (error) { 783 m = m0 = NULL; 784 goto bad; 785 } 786 787 for (; m; m = m0) { 788 m0 = m->m_nextpkt; 789 m->m_nextpkt = 0; 790 if (error == 0) 791 error = (*ifp->if_output)(ifp, m, sintosa(dst), 792 ro->ro_rt); 793 else 794 m_freem(m); 795 } 796 797 if (error == 0) 798 ipstat.ips_fragmented++; 799 800 done: 801 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) 802 RTFREE(ro->ro_rt); 803 return (error); 804 bad: 805 #ifdef IPSEC 806 if (error == EMSGSIZE && ip_mtudisc && icmp_mtu != 0 && m != NULL) 807 ipsec_adjust_mtu(m, icmp_mtu); 808 #endif 809 m_freem(m0); 810 goto done; 811 } 812 813 int 814 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu) 815 { 816 struct ip *ip, *mhip; 817 struct mbuf *m0; 818 int len, hlen, off; 819 int mhlen, firstlen; 820 struct mbuf **mnext; 821 int fragments = 0; 822 int error = 0; 823 824 ip = mtod(m, struct ip *); 825 hlen = ip->ip_hl << 2; 826 827 len = (mtu - hlen) &~ 7; 828 if (len < 8) { 829 m_freem(m); 830 return (EMSGSIZE); 831 } 832 833 /* 834 * If we are doing fragmentation, we can't defer TCP/UDP 835 * checksumming; compute the checksum and clear the flag. 836 */ 837 in_proto_cksum_out(m, NULL); 838 firstlen = len; 839 mnext = &m->m_nextpkt; 840 841 /* 842 * Loop through length of segment after first fragment, 843 * make new header and copy data of each part and link onto chain. 844 */ 845 m0 = m; 846 mhlen = sizeof (struct ip); 847 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) { 848 MGETHDR(m, M_DONTWAIT, MT_HEADER); 849 if (m == 0) { 850 ipstat.ips_odropped++; 851 error = ENOBUFS; 852 goto sendorfree; 853 } 854 *mnext = m; 855 mnext = &m->m_nextpkt; 856 m->m_data += max_linkhdr; 857 mhip = mtod(m, struct ip *); 858 *mhip = *ip; 859 /* we must inherit MCAST and BCAST flags and rdomain */ 860 m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST); 861 m->m_pkthdr.rdomain = m0->m_pkthdr.rdomain; 862 if (hlen > sizeof (struct ip)) { 863 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 864 mhip->ip_hl = mhlen >> 2; 865 } 866 m->m_len = mhlen; 867 mhip->ip_off = ((off - hlen) >> 3) + 868 (ntohs(ip->ip_off) & ~IP_MF); 869 if (ip->ip_off & htons(IP_MF)) 870 mhip->ip_off |= IP_MF; 871 if (off + len >= ntohs(ip->ip_len)) 872 len = ntohs(ip->ip_len) - off; 873 else 874 mhip->ip_off |= IP_MF; 875 mhip->ip_len = htons((u_int16_t)(len + mhlen)); 876 m->m_next = m_copy(m0, off, len); 877 if (m->m_next == 0) { 878 ipstat.ips_odropped++; 879 error = ENOBUFS; 880 goto sendorfree; 881 } 882 m->m_pkthdr.len = mhlen + len; 883 m->m_pkthdr.rcvif = (struct ifnet *)0; 884 mhip->ip_off = htons((u_int16_t)mhip->ip_off); 885 mhip->ip_sum = 0; 886 if ((ifp != NULL) && 887 (ifp->if_capabilities & IFCAP_CSUM_IPv4) && 888 (ifp->if_bridge == NULL)) { 889 m->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT; 890 ipstat.ips_outhwcsum++; 891 } else 892 mhip->ip_sum = in_cksum(m, mhlen); 893 ipstat.ips_ofragments++; 894 fragments++; 895 } 896 /* 897 * Update first fragment by trimming what's been copied out 898 * and updating header, then send each fragment (in order). 899 */ 900 m = m0; 901 m_adj(m, hlen + firstlen - ntohs(ip->ip_len)); 902 m->m_pkthdr.len = hlen + firstlen; 903 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len); 904 ip->ip_off |= htons(IP_MF); 905 ip->ip_sum = 0; 906 if ((ifp != NULL) && 907 (ifp->if_capabilities & IFCAP_CSUM_IPv4) && 908 (ifp->if_bridge == NULL)) { 909 m->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT; 910 ipstat.ips_outhwcsum++; 911 } else 912 ip->ip_sum = in_cksum(m, hlen); 913 sendorfree: 914 if (error) { 915 for (m = m0; m; m = m0) { 916 m0 = m->m_nextpkt; 917 m->m_nextpkt = NULL; 918 m_freem(m); 919 } 920 } 921 922 return (error); 923 } 924 925 /* 926 * Insert IP options into preformed packet. 927 * Adjust IP destination as required for IP source routing, 928 * as indicated by a non-zero in_addr at the start of the options. 929 */ 930 struct mbuf * 931 ip_insertoptions(m, opt, phlen) 932 struct mbuf *m; 933 struct mbuf *opt; 934 int *phlen; 935 { 936 struct ipoption *p = mtod(opt, struct ipoption *); 937 struct mbuf *n; 938 struct ip *ip = mtod(m, struct ip *); 939 unsigned int optlen; 940 941 optlen = opt->m_len - sizeof(p->ipopt_dst); 942 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) 943 return (m); /* XXX should fail */ 944 if (p->ipopt_dst.s_addr) 945 ip->ip_dst = p->ipopt_dst; 946 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 947 MGETHDR(n, M_DONTWAIT, MT_HEADER); 948 if (n == 0) 949 return (m); 950 M_MOVE_HDR(n, m); 951 n->m_pkthdr.len += optlen; 952 m->m_len -= sizeof(struct ip); 953 m->m_data += sizeof(struct ip); 954 n->m_next = m; 955 m = n; 956 m->m_len = optlen + sizeof(struct ip); 957 m->m_data += max_linkhdr; 958 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 959 } else { 960 m->m_data -= optlen; 961 m->m_len += optlen; 962 m->m_pkthdr.len += optlen; 963 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 964 } 965 ip = mtod(m, struct ip *); 966 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), optlen); 967 *phlen = sizeof(struct ip) + optlen; 968 ip->ip_len = htons(ntohs(ip->ip_len) + optlen); 969 return (m); 970 } 971 972 /* 973 * Copy options from ip to jp, 974 * omitting those not copied during fragmentation. 975 */ 976 int 977 ip_optcopy(ip, jp) 978 struct ip *ip, *jp; 979 { 980 u_char *cp, *dp; 981 int opt, optlen, cnt; 982 983 cp = (u_char *)(ip + 1); 984 dp = (u_char *)(jp + 1); 985 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 986 for (; cnt > 0; cnt -= optlen, cp += optlen) { 987 opt = cp[0]; 988 if (opt == IPOPT_EOL) 989 break; 990 if (opt == IPOPT_NOP) { 991 /* Preserve for IP mcast tunnel's LSRR alignment. */ 992 *dp++ = IPOPT_NOP; 993 optlen = 1; 994 continue; 995 } 996 #ifdef DIAGNOSTIC 997 if (cnt < IPOPT_OLEN + sizeof(*cp)) 998 panic("malformed IPv4 option passed to ip_optcopy"); 999 #endif 1000 optlen = cp[IPOPT_OLEN]; 1001 #ifdef DIAGNOSTIC 1002 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1003 panic("malformed IPv4 option passed to ip_optcopy"); 1004 #endif 1005 /* bogus lengths should have been caught by ip_dooptions */ 1006 if (optlen > cnt) 1007 optlen = cnt; 1008 if (IPOPT_COPIED(opt)) { 1009 bcopy((caddr_t)cp, (caddr_t)dp, optlen); 1010 dp += optlen; 1011 } 1012 } 1013 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1014 *dp++ = IPOPT_EOL; 1015 return (optlen); 1016 } 1017 1018 /* 1019 * IP socket option processing. 1020 */ 1021 int 1022 ip_ctloutput(op, so, level, optname, mp) 1023 int op; 1024 struct socket *so; 1025 int level, optname; 1026 struct mbuf **mp; 1027 { 1028 struct inpcb *inp = sotoinpcb(so); 1029 struct mbuf *m = *mp; 1030 int optval = 0; 1031 struct proc *p = curproc; /* XXX */ 1032 #ifdef IPSEC 1033 struct ipsec_ref *ipr; 1034 u_int16_t opt16val; 1035 #endif 1036 int error = 0; 1037 u_int rtid = 0; 1038 1039 if (level != IPPROTO_IP) { 1040 error = EINVAL; 1041 if (op == PRCO_SETOPT && *mp) 1042 (void) m_free(*mp); 1043 } else switch (op) { 1044 case PRCO_SETOPT: 1045 switch (optname) { 1046 case IP_OPTIONS: 1047 #ifdef notyet 1048 case IP_RETOPTS: 1049 return (ip_pcbopts(optname, &inp->inp_options, m)); 1050 #else 1051 return (ip_pcbopts(&inp->inp_options, m)); 1052 #endif 1053 1054 case IP_TOS: 1055 case IP_TTL: 1056 case IP_MINTTL: 1057 case IP_RECVOPTS: 1058 case IP_RECVRETOPTS: 1059 case IP_RECVDSTADDR: 1060 case IP_RECVIF: 1061 case IP_RECVTTL: 1062 case IP_RECVDSTPORT: 1063 case IP_RECVRTABLE: 1064 if (m == NULL || m->m_len != sizeof(int)) 1065 error = EINVAL; 1066 else { 1067 optval = *mtod(m, int *); 1068 switch (optname) { 1069 1070 case IP_TOS: 1071 inp->inp_ip.ip_tos = optval; 1072 break; 1073 1074 case IP_TTL: 1075 if (optval > 0 && optval <= MAXTTL) 1076 inp->inp_ip.ip_ttl = optval; 1077 else 1078 error = EINVAL; 1079 break; 1080 1081 case IP_MINTTL: 1082 if (optval > 0 && optval <= MAXTTL) 1083 inp->inp_ip_minttl = optval; 1084 else 1085 error = EINVAL; 1086 break; 1087 #define OPTSET(bit) \ 1088 if (optval) \ 1089 inp->inp_flags |= bit; \ 1090 else \ 1091 inp->inp_flags &= ~bit; 1092 1093 case IP_RECVOPTS: 1094 OPTSET(INP_RECVOPTS); 1095 break; 1096 1097 case IP_RECVRETOPTS: 1098 OPTSET(INP_RECVRETOPTS); 1099 break; 1100 1101 case IP_RECVDSTADDR: 1102 OPTSET(INP_RECVDSTADDR); 1103 break; 1104 case IP_RECVIF: 1105 OPTSET(INP_RECVIF); 1106 break; 1107 case IP_RECVTTL: 1108 OPTSET(INP_RECVTTL); 1109 break; 1110 case IP_RECVDSTPORT: 1111 OPTSET(INP_RECVDSTPORT); 1112 break; 1113 case IP_RECVRTABLE: 1114 OPTSET(INP_RECVRTABLE); 1115 break; 1116 } 1117 } 1118 break; 1119 #undef OPTSET 1120 1121 case IP_MULTICAST_IF: 1122 case IP_MULTICAST_TTL: 1123 case IP_MULTICAST_LOOP: 1124 case IP_ADD_MEMBERSHIP: 1125 case IP_DROP_MEMBERSHIP: 1126 error = ip_setmoptions(optname, &inp->inp_moptions, m, 1127 inp->inp_rtableid); 1128 break; 1129 1130 case IP_PORTRANGE: 1131 if (m == 0 || m->m_len != sizeof(int)) 1132 error = EINVAL; 1133 else { 1134 optval = *mtod(m, int *); 1135 1136 switch (optval) { 1137 1138 case IP_PORTRANGE_DEFAULT: 1139 inp->inp_flags &= ~(INP_LOWPORT); 1140 inp->inp_flags &= ~(INP_HIGHPORT); 1141 break; 1142 1143 case IP_PORTRANGE_HIGH: 1144 inp->inp_flags &= ~(INP_LOWPORT); 1145 inp->inp_flags |= INP_HIGHPORT; 1146 break; 1147 1148 case IP_PORTRANGE_LOW: 1149 inp->inp_flags &= ~(INP_HIGHPORT); 1150 inp->inp_flags |= INP_LOWPORT; 1151 break; 1152 1153 default: 1154 1155 error = EINVAL; 1156 break; 1157 } 1158 } 1159 break; 1160 case IP_AUTH_LEVEL: 1161 case IP_ESP_TRANS_LEVEL: 1162 case IP_ESP_NETWORK_LEVEL: 1163 case IP_IPCOMP_LEVEL: 1164 #ifndef IPSEC 1165 error = EOPNOTSUPP; 1166 #else 1167 if (m == 0 || m->m_len != sizeof(int)) { 1168 error = EINVAL; 1169 break; 1170 } 1171 optval = *mtod(m, int *); 1172 1173 if (optval < IPSEC_LEVEL_BYPASS || 1174 optval > IPSEC_LEVEL_UNIQUE) { 1175 error = EINVAL; 1176 break; 1177 } 1178 1179 /* Unlink cached output TDB to force a re-search */ 1180 if (inp->inp_tdb_out) { 1181 int s = spltdb(); 1182 TAILQ_REMOVE(&inp->inp_tdb_out->tdb_inp_out, 1183 inp, inp_tdb_out_next); 1184 splx(s); 1185 } 1186 1187 if (inp->inp_tdb_in) { 1188 int s = spltdb(); 1189 TAILQ_REMOVE(&inp->inp_tdb_in->tdb_inp_in, 1190 inp, inp_tdb_in_next); 1191 splx(s); 1192 } 1193 1194 switch (optname) { 1195 case IP_AUTH_LEVEL: 1196 if (optval < ipsec_auth_default_level && 1197 suser(p, 0)) { 1198 error = EACCES; 1199 break; 1200 } 1201 inp->inp_seclevel[SL_AUTH] = optval; 1202 break; 1203 1204 case IP_ESP_TRANS_LEVEL: 1205 if (optval < ipsec_esp_trans_default_level && 1206 suser(p, 0)) { 1207 error = EACCES; 1208 break; 1209 } 1210 inp->inp_seclevel[SL_ESP_TRANS] = optval; 1211 break; 1212 1213 case IP_ESP_NETWORK_LEVEL: 1214 if (optval < ipsec_esp_network_default_level && 1215 suser(p, 0)) { 1216 error = EACCES; 1217 break; 1218 } 1219 inp->inp_seclevel[SL_ESP_NETWORK] = optval; 1220 break; 1221 case IP_IPCOMP_LEVEL: 1222 if (optval < ipsec_ipcomp_default_level && 1223 suser(p, 0)) { 1224 error = EACCES; 1225 break; 1226 } 1227 inp->inp_seclevel[SL_IPCOMP] = optval; 1228 break; 1229 } 1230 if (!error) 1231 inp->inp_secrequire = get_sa_require(inp); 1232 #endif 1233 break; 1234 1235 case IP_IPSEC_REMOTE_CRED: 1236 case IP_IPSEC_REMOTE_AUTH: 1237 /* Can't set the remote credential or key */ 1238 error = EOPNOTSUPP; 1239 break; 1240 1241 case IP_IPSEC_LOCAL_ID: 1242 case IP_IPSEC_REMOTE_ID: 1243 case IP_IPSEC_LOCAL_CRED: 1244 case IP_IPSEC_LOCAL_AUTH: 1245 #ifndef IPSEC 1246 error = EOPNOTSUPP; 1247 #else 1248 if (m == NULL || m->m_len < 2) { 1249 error = EINVAL; 1250 break; 1251 } 1252 1253 m_copydata(m, 0, 2, (caddr_t) &opt16val); 1254 1255 /* If the type is 0, then we cleanup and return */ 1256 if (opt16val == 0) { 1257 switch (optname) { 1258 case IP_IPSEC_LOCAL_ID: 1259 if (inp->inp_ipo != NULL && 1260 inp->inp_ipo->ipo_srcid != NULL) { 1261 ipsp_reffree(inp->inp_ipo->ipo_srcid); 1262 inp->inp_ipo->ipo_srcid = NULL; 1263 } 1264 break; 1265 1266 case IP_IPSEC_REMOTE_ID: 1267 if (inp->inp_ipo != NULL && 1268 inp->inp_ipo->ipo_dstid != NULL) { 1269 ipsp_reffree(inp->inp_ipo->ipo_dstid); 1270 inp->inp_ipo->ipo_dstid = NULL; 1271 } 1272 break; 1273 1274 case IP_IPSEC_LOCAL_CRED: 1275 if (inp->inp_ipo != NULL && 1276 inp->inp_ipo->ipo_local_cred != NULL) { 1277 ipsp_reffree(inp->inp_ipo->ipo_local_cred); 1278 inp->inp_ipo->ipo_local_cred = NULL; 1279 } 1280 break; 1281 1282 case IP_IPSEC_LOCAL_AUTH: 1283 if (inp->inp_ipo != NULL && 1284 inp->inp_ipo->ipo_local_auth != NULL) { 1285 ipsp_reffree(inp->inp_ipo->ipo_local_auth); 1286 inp->inp_ipo->ipo_local_auth = NULL; 1287 } 1288 break; 1289 } 1290 1291 error = 0; 1292 break; 1293 } 1294 1295 /* Can't have an empty payload */ 1296 if (m->m_len == 2) { 1297 error = EINVAL; 1298 break; 1299 } 1300 1301 /* Allocate if needed */ 1302 if (inp->inp_ipo == NULL) { 1303 inp->inp_ipo = ipsec_add_policy(inp, 1304 AF_INET, IPSP_DIRECTION_OUT); 1305 if (inp->inp_ipo == NULL) { 1306 error = ENOBUFS; 1307 break; 1308 } 1309 } 1310 1311 ipr = malloc(sizeof(struct ipsec_ref) + m->m_len - 2, 1312 M_CREDENTIALS, M_NOWAIT); 1313 if (ipr == NULL) { 1314 error = ENOBUFS; 1315 break; 1316 } 1317 1318 ipr->ref_count = 1; 1319 ipr->ref_malloctype = M_CREDENTIALS; 1320 ipr->ref_len = m->m_len - 2; 1321 ipr->ref_type = opt16val; 1322 m_copydata(m, 2, m->m_len - 2, (caddr_t)(ipr + 1)); 1323 1324 switch (optname) { 1325 case IP_IPSEC_LOCAL_ID: 1326 /* Check valid types and NUL-termination */ 1327 if (ipr->ref_type < IPSP_IDENTITY_PREFIX || 1328 ipr->ref_type > IPSP_IDENTITY_CONNECTION || 1329 ((char *)(ipr + 1))[ipr->ref_len - 1]) { 1330 free(ipr, M_CREDENTIALS); 1331 error = EINVAL; 1332 } else { 1333 if (inp->inp_ipo->ipo_srcid != NULL) 1334 ipsp_reffree(inp->inp_ipo->ipo_srcid); 1335 inp->inp_ipo->ipo_srcid = ipr; 1336 } 1337 break; 1338 case IP_IPSEC_REMOTE_ID: 1339 /* Check valid types and NUL-termination */ 1340 if (ipr->ref_type < IPSP_IDENTITY_PREFIX || 1341 ipr->ref_type > IPSP_IDENTITY_CONNECTION || 1342 ((char *)(ipr + 1))[ipr->ref_len - 1]) { 1343 free(ipr, M_CREDENTIALS); 1344 error = EINVAL; 1345 } else { 1346 if (inp->inp_ipo->ipo_dstid != NULL) 1347 ipsp_reffree(inp->inp_ipo->ipo_dstid); 1348 inp->inp_ipo->ipo_dstid = ipr; 1349 } 1350 break; 1351 case IP_IPSEC_LOCAL_CRED: 1352 if (ipr->ref_type < IPSP_CRED_KEYNOTE || 1353 ipr->ref_type > IPSP_CRED_X509) { 1354 free(ipr, M_CREDENTIALS); 1355 error = EINVAL; 1356 } else { 1357 if (inp->inp_ipo->ipo_local_cred != NULL) 1358 ipsp_reffree(inp->inp_ipo->ipo_local_cred); 1359 inp->inp_ipo->ipo_local_cred = ipr; 1360 } 1361 break; 1362 case IP_IPSEC_LOCAL_AUTH: 1363 if (ipr->ref_type < IPSP_AUTH_PASSPHRASE || 1364 ipr->ref_type > IPSP_AUTH_RSA) { 1365 free(ipr, M_CREDENTIALS); 1366 error = EINVAL; 1367 } else { 1368 if (inp->inp_ipo->ipo_local_auth != NULL) 1369 ipsp_reffree(inp->inp_ipo->ipo_local_auth); 1370 inp->inp_ipo->ipo_local_auth = ipr; 1371 } 1372 break; 1373 } 1374 1375 /* Unlink cached output TDB to force a re-search */ 1376 if (inp->inp_tdb_out) { 1377 int s = spltdb(); 1378 TAILQ_REMOVE(&inp->inp_tdb_out->tdb_inp_out, 1379 inp, inp_tdb_out_next); 1380 splx(s); 1381 } 1382 1383 if (inp->inp_tdb_in) { 1384 int s = spltdb(); 1385 TAILQ_REMOVE(&inp->inp_tdb_in->tdb_inp_in, 1386 inp, inp_tdb_in_next); 1387 splx(s); 1388 } 1389 #endif 1390 break; 1391 case SO_RTABLE: 1392 if (m == NULL || m->m_len < sizeof(u_int)) { 1393 error = EINVAL; 1394 break; 1395 } 1396 rtid = *mtod(m, u_int *); 1397 if (inp->inp_rtableid == rtid) 1398 break; 1399 /* needs priviledges to switch when already set */ 1400 if (p->p_p->ps_rtableid != rtid && 1401 p->p_p->ps_rtableid != 0 && 1402 (error = suser(p, 0)) != 0) 1403 break; 1404 /* table must exist */ 1405 if (!rtable_exists(rtid)) { 1406 error = EINVAL; 1407 break; 1408 } 1409 inp->inp_rtableid = rtid; 1410 break; 1411 case IP_PIPEX: 1412 if (m != NULL && m->m_len == sizeof(int)) 1413 inp->inp_pipex = *mtod(m, int *); 1414 else 1415 error = EINVAL; 1416 break; 1417 1418 default: 1419 error = ENOPROTOOPT; 1420 break; 1421 } 1422 if (m) 1423 (void)m_free(m); 1424 break; 1425 1426 case PRCO_GETOPT: 1427 switch (optname) { 1428 case IP_OPTIONS: 1429 case IP_RETOPTS: 1430 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1431 if (inp->inp_options) { 1432 m->m_len = inp->inp_options->m_len; 1433 bcopy(mtod(inp->inp_options, caddr_t), 1434 mtod(m, caddr_t), m->m_len); 1435 } else 1436 m->m_len = 0; 1437 break; 1438 1439 case IP_TOS: 1440 case IP_TTL: 1441 case IP_MINTTL: 1442 case IP_RECVOPTS: 1443 case IP_RECVRETOPTS: 1444 case IP_RECVDSTADDR: 1445 case IP_RECVIF: 1446 case IP_RECVTTL: 1447 case IP_RECVDSTPORT: 1448 case IP_RECVRTABLE: 1449 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1450 m->m_len = sizeof(int); 1451 switch (optname) { 1452 1453 case IP_TOS: 1454 optval = inp->inp_ip.ip_tos; 1455 break; 1456 1457 case IP_TTL: 1458 optval = inp->inp_ip.ip_ttl; 1459 break; 1460 1461 case IP_MINTTL: 1462 optval = inp->inp_ip_minttl; 1463 break; 1464 1465 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1466 1467 case IP_RECVOPTS: 1468 optval = OPTBIT(INP_RECVOPTS); 1469 break; 1470 1471 case IP_RECVRETOPTS: 1472 optval = OPTBIT(INP_RECVRETOPTS); 1473 break; 1474 1475 case IP_RECVDSTADDR: 1476 optval = OPTBIT(INP_RECVDSTADDR); 1477 break; 1478 case IP_RECVIF: 1479 optval = OPTBIT(INP_RECVIF); 1480 break; 1481 case IP_RECVTTL: 1482 optval = OPTBIT(INP_RECVTTL); 1483 break; 1484 case IP_RECVDSTPORT: 1485 optval = OPTBIT(INP_RECVDSTPORT); 1486 break; 1487 case IP_RECVRTABLE: 1488 optval = OPTBIT(INP_RECVRTABLE); 1489 break; 1490 } 1491 *mtod(m, int *) = optval; 1492 break; 1493 1494 case IP_MULTICAST_IF: 1495 case IP_MULTICAST_TTL: 1496 case IP_MULTICAST_LOOP: 1497 case IP_ADD_MEMBERSHIP: 1498 case IP_DROP_MEMBERSHIP: 1499 error = ip_getmoptions(optname, inp->inp_moptions, mp); 1500 break; 1501 1502 case IP_PORTRANGE: 1503 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1504 m->m_len = sizeof(int); 1505 1506 if (inp->inp_flags & INP_HIGHPORT) 1507 optval = IP_PORTRANGE_HIGH; 1508 else if (inp->inp_flags & INP_LOWPORT) 1509 optval = IP_PORTRANGE_LOW; 1510 else 1511 optval = 0; 1512 1513 *mtod(m, int *) = optval; 1514 break; 1515 1516 case IP_AUTH_LEVEL: 1517 case IP_ESP_TRANS_LEVEL: 1518 case IP_ESP_NETWORK_LEVEL: 1519 case IP_IPCOMP_LEVEL: 1520 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1521 #ifndef IPSEC 1522 m->m_len = sizeof(int); 1523 *mtod(m, int *) = IPSEC_LEVEL_NONE; 1524 #else 1525 m->m_len = sizeof(int); 1526 switch (optname) { 1527 case IP_AUTH_LEVEL: 1528 optval = inp->inp_seclevel[SL_AUTH]; 1529 break; 1530 1531 case IP_ESP_TRANS_LEVEL: 1532 optval = inp->inp_seclevel[SL_ESP_TRANS]; 1533 break; 1534 1535 case IP_ESP_NETWORK_LEVEL: 1536 optval = inp->inp_seclevel[SL_ESP_NETWORK]; 1537 break; 1538 case IP_IPCOMP_LEVEL: 1539 optval = inp->inp_seclevel[SL_IPCOMP]; 1540 break; 1541 } 1542 *mtod(m, int *) = optval; 1543 #endif 1544 break; 1545 case IP_IPSEC_LOCAL_ID: 1546 case IP_IPSEC_REMOTE_ID: 1547 case IP_IPSEC_LOCAL_CRED: 1548 case IP_IPSEC_REMOTE_CRED: 1549 case IP_IPSEC_LOCAL_AUTH: 1550 case IP_IPSEC_REMOTE_AUTH: 1551 #ifndef IPSEC 1552 error = EOPNOTSUPP; 1553 #else 1554 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1555 m->m_len = sizeof(u_int16_t); 1556 ipr = NULL; 1557 switch (optname) { 1558 case IP_IPSEC_LOCAL_ID: 1559 if (inp->inp_ipo != NULL) 1560 ipr = inp->inp_ipo->ipo_srcid; 1561 opt16val = IPSP_IDENTITY_NONE; 1562 break; 1563 case IP_IPSEC_REMOTE_ID: 1564 if (inp->inp_ipo != NULL) 1565 ipr = inp->inp_ipo->ipo_dstid; 1566 opt16val = IPSP_IDENTITY_NONE; 1567 break; 1568 case IP_IPSEC_LOCAL_CRED: 1569 if (inp->inp_ipo != NULL) 1570 ipr = inp->inp_ipo->ipo_local_cred; 1571 opt16val = IPSP_CRED_NONE; 1572 break; 1573 case IP_IPSEC_REMOTE_CRED: 1574 ipr = inp->inp_ipsec_remotecred; 1575 opt16val = IPSP_CRED_NONE; 1576 break; 1577 case IP_IPSEC_LOCAL_AUTH: 1578 if (inp->inp_ipo != NULL) 1579 ipr = inp->inp_ipo->ipo_local_auth; 1580 opt16val = IPSP_AUTH_NONE; 1581 break; 1582 case IP_IPSEC_REMOTE_AUTH: 1583 ipr = inp->inp_ipsec_remoteauth; 1584 opt16val = IPSP_AUTH_NONE; 1585 break; 1586 } 1587 if (ipr == NULL) 1588 *mtod(m, u_int16_t *) = opt16val; 1589 else { 1590 size_t len; 1591 1592 len = m->m_len + ipr->ref_len; 1593 if (len > MCLBYTES) { 1594 m_free(m); 1595 error = EINVAL; 1596 break; 1597 } 1598 /* allocate mbuf cluster for larger option */ 1599 if (len > MLEN) { 1600 MCLGET(m, M_WAITOK); 1601 if ((m->m_flags & M_EXT) == 0) { 1602 m_free(m); 1603 error = ENOBUFS; 1604 break; 1605 } 1606 1607 } 1608 m->m_len = len; 1609 *mtod(m, u_int16_t *) = ipr->ref_type; 1610 m_copyback(m, sizeof(u_int16_t), ipr->ref_len, 1611 ipr + 1, M_NOWAIT); 1612 } 1613 #endif 1614 break; 1615 case SO_RTABLE: 1616 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1617 m->m_len = sizeof(u_int); 1618 *mtod(m, u_int *) = inp->inp_rtableid; 1619 break; 1620 case IP_PIPEX: 1621 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1622 m->m_len = sizeof(int); 1623 *mtod(m, int *) = inp->inp_pipex; 1624 break; 1625 default: 1626 error = ENOPROTOOPT; 1627 break; 1628 } 1629 break; 1630 } 1631 return (error); 1632 } 1633 1634 /* 1635 * Set up IP options in pcb for insertion in output packets. 1636 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1637 * with destination address if source routed. 1638 */ 1639 int 1640 #ifdef notyet 1641 ip_pcbopts(optname, pcbopt, m) 1642 int optname; 1643 #else 1644 ip_pcbopts(pcbopt, m) 1645 #endif 1646 struct mbuf **pcbopt; 1647 struct mbuf *m; 1648 { 1649 int cnt, optlen; 1650 u_char *cp; 1651 u_char opt; 1652 1653 /* turn off any old options */ 1654 if (*pcbopt) 1655 (void)m_free(*pcbopt); 1656 *pcbopt = 0; 1657 if (m == (struct mbuf *)0 || m->m_len == 0) { 1658 /* 1659 * Only turning off any previous options. 1660 */ 1661 if (m) 1662 (void)m_free(m); 1663 return (0); 1664 } 1665 1666 if (m->m_len % sizeof(int32_t)) 1667 goto bad; 1668 1669 /* 1670 * IP first-hop destination address will be stored before 1671 * actual options; move other options back 1672 * and clear it when none present. 1673 */ 1674 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1675 goto bad; 1676 cnt = m->m_len; 1677 m->m_len += sizeof(struct in_addr); 1678 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1679 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); 1680 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 1681 1682 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1683 opt = cp[IPOPT_OPTVAL]; 1684 if (opt == IPOPT_EOL) 1685 break; 1686 if (opt == IPOPT_NOP) 1687 optlen = 1; 1688 else { 1689 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1690 goto bad; 1691 optlen = cp[IPOPT_OLEN]; 1692 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1693 goto bad; 1694 } 1695 switch (opt) { 1696 1697 default: 1698 break; 1699 1700 case IPOPT_LSRR: 1701 case IPOPT_SSRR: 1702 /* 1703 * user process specifies route as: 1704 * ->A->B->C->D 1705 * D must be our final destination (but we can't 1706 * check that since we may not have connected yet). 1707 * A is first hop destination, which doesn't appear in 1708 * actual IP option, but is stored before the options. 1709 */ 1710 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1711 goto bad; 1712 m->m_len -= sizeof(struct in_addr); 1713 cnt -= sizeof(struct in_addr); 1714 optlen -= sizeof(struct in_addr); 1715 cp[IPOPT_OLEN] = optlen; 1716 /* 1717 * Move first hop before start of options. 1718 */ 1719 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1720 sizeof(struct in_addr)); 1721 /* 1722 * Then copy rest of options back 1723 * to close up the deleted entry. 1724 */ 1725 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + 1726 sizeof(struct in_addr)), 1727 (caddr_t)&cp[IPOPT_OFFSET+1], 1728 (unsigned)cnt - (IPOPT_OFFSET+1)); 1729 break; 1730 } 1731 } 1732 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1733 goto bad; 1734 *pcbopt = m; 1735 return (0); 1736 1737 bad: 1738 (void)m_free(m); 1739 return (EINVAL); 1740 } 1741 1742 /* 1743 * Set the IP multicast options in response to user setsockopt(). 1744 */ 1745 int 1746 ip_setmoptions(int optname, struct ip_moptions **imop, struct mbuf *m, 1747 u_int rtableid) 1748 { 1749 int error = 0; 1750 u_char loop; 1751 int i; 1752 struct in_addr addr; 1753 struct ip_mreq *mreq; 1754 struct ifnet *ifp; 1755 struct ip_moptions *imo = *imop; 1756 struct in_multi **immp; 1757 struct route ro; 1758 struct sockaddr_in *dst; 1759 1760 if (imo == NULL) { 1761 /* 1762 * No multicast option buffer attached to the pcb; 1763 * allocate one and initialize to default values. 1764 */ 1765 imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS, 1766 M_WAITOK|M_ZERO); 1767 immp = (struct in_multi **)malloc( 1768 (sizeof(*immp) * IP_MIN_MEMBERSHIPS), M_IPMOPTS, 1769 M_WAITOK|M_ZERO); 1770 *imop = imo; 1771 imo->imo_multicast_ifp = NULL; 1772 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1773 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1774 imo->imo_num_memberships = 0; 1775 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS; 1776 imo->imo_membership = immp; 1777 } 1778 1779 switch (optname) { 1780 1781 case IP_MULTICAST_IF: 1782 /* 1783 * Select the interface for outgoing multicast packets. 1784 */ 1785 if (m == NULL || m->m_len != sizeof(struct in_addr)) { 1786 error = EINVAL; 1787 break; 1788 } 1789 addr = *(mtod(m, struct in_addr *)); 1790 /* 1791 * INADDR_ANY is used to remove a previous selection. 1792 * When no interface is selected, a default one is 1793 * chosen every time a multicast packet is sent. 1794 */ 1795 if (addr.s_addr == INADDR_ANY) { 1796 imo->imo_multicast_ifp = NULL; 1797 break; 1798 } 1799 /* 1800 * The selected interface is identified by its local 1801 * IP address. Find the interface and confirm that 1802 * it supports multicasting. 1803 */ 1804 INADDR_TO_IFP(addr, ifp, rtableid); 1805 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1806 error = EADDRNOTAVAIL; 1807 break; 1808 } 1809 imo->imo_multicast_ifp = ifp; 1810 break; 1811 1812 case IP_MULTICAST_TTL: 1813 /* 1814 * Set the IP time-to-live for outgoing multicast packets. 1815 */ 1816 if (m == NULL || m->m_len != 1) { 1817 error = EINVAL; 1818 break; 1819 } 1820 imo->imo_multicast_ttl = *(mtod(m, u_char *)); 1821 break; 1822 1823 case IP_MULTICAST_LOOP: 1824 /* 1825 * Set the loopback flag for outgoing multicast packets. 1826 * Must be zero or one. 1827 */ 1828 if (m == NULL || m->m_len != 1 || 1829 (loop = *(mtod(m, u_char *))) > 1) { 1830 error = EINVAL; 1831 break; 1832 } 1833 imo->imo_multicast_loop = loop; 1834 break; 1835 1836 case IP_ADD_MEMBERSHIP: 1837 /* 1838 * Add a multicast group membership. 1839 * Group must be a valid IP multicast address. 1840 */ 1841 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 1842 error = EINVAL; 1843 break; 1844 } 1845 mreq = mtod(m, struct ip_mreq *); 1846 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) { 1847 error = EINVAL; 1848 break; 1849 } 1850 /* 1851 * If no interface address was provided, use the interface of 1852 * the route to the given multicast address. 1853 */ 1854 if (mreq->imr_interface.s_addr == INADDR_ANY) { 1855 ro.ro_rt = NULL; 1856 dst = satosin(&ro.ro_dst); 1857 dst->sin_len = sizeof(*dst); 1858 dst->sin_family = AF_INET; 1859 dst->sin_addr = mreq->imr_multiaddr; 1860 if (!(ro.ro_rt && ro.ro_rt->rt_ifp && 1861 (ro.ro_rt->rt_flags & RTF_UP))) 1862 ro.ro_rt = rtalloc1(&ro.ro_dst, RT_REPORT, 1863 rtableid); 1864 if (ro.ro_rt == NULL) { 1865 error = EADDRNOTAVAIL; 1866 break; 1867 } 1868 ifp = ro.ro_rt->rt_ifp; 1869 rtfree(ro.ro_rt); 1870 } else { 1871 INADDR_TO_IFP(mreq->imr_interface, ifp, rtableid); 1872 } 1873 /* 1874 * See if we found an interface, and confirm that it 1875 * supports multicast. 1876 */ 1877 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1878 error = EADDRNOTAVAIL; 1879 break; 1880 } 1881 /* 1882 * See if the membership already exists or if all the 1883 * membership slots are full. 1884 */ 1885 for (i = 0; i < imo->imo_num_memberships; ++i) { 1886 if (imo->imo_membership[i]->inm_ia->ia_ifp == ifp && 1887 imo->imo_membership[i]->inm_addr.s_addr 1888 == mreq->imr_multiaddr.s_addr) 1889 break; 1890 } 1891 if (i < imo->imo_num_memberships) { 1892 error = EADDRINUSE; 1893 break; 1894 } 1895 if (imo->imo_num_memberships == imo->imo_max_memberships) { 1896 struct in_multi **nmships, **omships; 1897 size_t newmax; 1898 /* 1899 * Resize the vector to next power-of-two minus 1. If the 1900 * size would exceed the maximum then we know we've really 1901 * run out of entries. Otherwise, we reallocate the vector. 1902 */ 1903 nmships = NULL; 1904 omships = imo->imo_membership; 1905 newmax = ((imo->imo_max_memberships + 1) * 2) - 1; 1906 if (newmax <= IP_MAX_MEMBERSHIPS) { 1907 nmships = (struct in_multi **)malloc( 1908 sizeof(*nmships) * newmax, M_IPMOPTS, 1909 M_NOWAIT|M_ZERO); 1910 if (nmships != NULL) { 1911 bcopy(omships, nmships, 1912 sizeof(*omships) * 1913 imo->imo_max_memberships); 1914 free(omships, M_IPMOPTS); 1915 imo->imo_membership = nmships; 1916 imo->imo_max_memberships = newmax; 1917 } 1918 } 1919 if (nmships == NULL) { 1920 error = ETOOMANYREFS; 1921 break; 1922 } 1923 } 1924 /* 1925 * Everything looks good; add a new record to the multicast 1926 * address list for the given interface. 1927 */ 1928 if ((imo->imo_membership[i] = 1929 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) { 1930 error = ENOBUFS; 1931 break; 1932 } 1933 ++imo->imo_num_memberships; 1934 break; 1935 1936 case IP_DROP_MEMBERSHIP: 1937 /* 1938 * Drop a multicast group membership. 1939 * Group must be a valid IP multicast address. 1940 */ 1941 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 1942 error = EINVAL; 1943 break; 1944 } 1945 mreq = mtod(m, struct ip_mreq *); 1946 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) { 1947 error = EINVAL; 1948 break; 1949 } 1950 /* 1951 * If an interface address was specified, get a pointer 1952 * to its ifnet structure. 1953 */ 1954 if (mreq->imr_interface.s_addr == INADDR_ANY) 1955 ifp = NULL; 1956 else { 1957 INADDR_TO_IFP(mreq->imr_interface, ifp, rtableid); 1958 if (ifp == NULL) { 1959 error = EADDRNOTAVAIL; 1960 break; 1961 } 1962 } 1963 /* 1964 * Find the membership in the membership array. 1965 */ 1966 for (i = 0; i < imo->imo_num_memberships; ++i) { 1967 if ((ifp == NULL || 1968 imo->imo_membership[i]->inm_ia->ia_ifp == ifp) && 1969 imo->imo_membership[i]->inm_addr.s_addr == 1970 mreq->imr_multiaddr.s_addr) 1971 break; 1972 } 1973 if (i == imo->imo_num_memberships) { 1974 error = EADDRNOTAVAIL; 1975 break; 1976 } 1977 /* 1978 * Give up the multicast address record to which the 1979 * membership points. 1980 */ 1981 in_delmulti(imo->imo_membership[i]); 1982 /* 1983 * Remove the gap in the membership array. 1984 */ 1985 for (++i; i < imo->imo_num_memberships; ++i) 1986 imo->imo_membership[i-1] = imo->imo_membership[i]; 1987 --imo->imo_num_memberships; 1988 break; 1989 1990 default: 1991 error = EOPNOTSUPP; 1992 break; 1993 } 1994 1995 /* 1996 * If all options have default values, no need to keep the data. 1997 */ 1998 if (imo->imo_multicast_ifp == NULL && 1999 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 2000 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 2001 imo->imo_num_memberships == 0) { 2002 free(imo->imo_membership , M_IPMOPTS); 2003 free(*imop, M_IPMOPTS); 2004 *imop = NULL; 2005 } 2006 2007 return (error); 2008 } 2009 2010 /* 2011 * Return the IP multicast options in response to user getsockopt(). 2012 */ 2013 int 2014 ip_getmoptions(optname, imo, mp) 2015 int optname; 2016 struct ip_moptions *imo; 2017 struct mbuf **mp; 2018 { 2019 u_char *ttl; 2020 u_char *loop; 2021 struct in_addr *addr; 2022 struct in_ifaddr *ia; 2023 2024 *mp = m_get(M_WAIT, MT_SOOPTS); 2025 2026 switch (optname) { 2027 2028 case IP_MULTICAST_IF: 2029 addr = mtod(*mp, struct in_addr *); 2030 (*mp)->m_len = sizeof(struct in_addr); 2031 if (imo == NULL || imo->imo_multicast_ifp == NULL) 2032 addr->s_addr = INADDR_ANY; 2033 else { 2034 IFP_TO_IA(imo->imo_multicast_ifp, ia); 2035 addr->s_addr = (ia == NULL) ? INADDR_ANY 2036 : ia->ia_addr.sin_addr.s_addr; 2037 } 2038 return (0); 2039 2040 case IP_MULTICAST_TTL: 2041 ttl = mtod(*mp, u_char *); 2042 (*mp)->m_len = 1; 2043 *ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL 2044 : imo->imo_multicast_ttl; 2045 return (0); 2046 2047 case IP_MULTICAST_LOOP: 2048 loop = mtod(*mp, u_char *); 2049 (*mp)->m_len = 1; 2050 *loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP 2051 : imo->imo_multicast_loop; 2052 return (0); 2053 2054 default: 2055 return (EOPNOTSUPP); 2056 } 2057 } 2058 2059 /* 2060 * Discard the IP multicast options. 2061 */ 2062 void 2063 ip_freemoptions(imo) 2064 struct ip_moptions *imo; 2065 { 2066 int i; 2067 2068 if (imo != NULL) { 2069 for (i = 0; i < imo->imo_num_memberships; ++i) 2070 in_delmulti(imo->imo_membership[i]); 2071 free(imo->imo_membership, M_IPMOPTS); 2072 free(imo, M_IPMOPTS); 2073 } 2074 } 2075 2076 /* 2077 * Routine called from ip_output() to loop back a copy of an IP multicast 2078 * packet to the input queue of a specified interface. Note that this 2079 * calls the output routine of the loopback "driver", but with an interface 2080 * pointer that might NOT be &loif -- easier than replicating that code here. 2081 */ 2082 void 2083 ip_mloopback(ifp, m, dst) 2084 struct ifnet *ifp; 2085 struct mbuf *m; 2086 struct sockaddr_in *dst; 2087 { 2088 struct ip *ip; 2089 struct mbuf *copym; 2090 2091 copym = m_copym2(m, 0, M_COPYALL, M_DONTWAIT); 2092 if (copym != NULL) { 2093 /* 2094 * We don't bother to fragment if the IP length is greater 2095 * than the interface's MTU. Can this possibly matter? 2096 */ 2097 ip = mtod(copym, struct ip *); 2098 ip->ip_sum = 0; 2099 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2); 2100 (void) looutput(ifp, copym, sintosa(dst), NULL); 2101 } 2102 } 2103 2104 /* 2105 * Process a delayed payload checksum calculation. 2106 */ 2107 void 2108 in_delayed_cksum(struct mbuf *m) 2109 { 2110 struct ip *ip; 2111 u_int16_t csum, offset; 2112 2113 ip = mtod(m, struct ip *); 2114 offset = ip->ip_hl << 2; 2115 csum = in4_cksum(m, 0, offset, m->m_pkthdr.len - offset); 2116 if (csum == 0 && ip->ip_p == IPPROTO_UDP) 2117 csum = 0xffff; 2118 2119 switch (ip->ip_p) { 2120 case IPPROTO_TCP: 2121 offset += offsetof(struct tcphdr, th_sum); 2122 break; 2123 2124 case IPPROTO_UDP: 2125 offset += offsetof(struct udphdr, uh_sum); 2126 break; 2127 2128 default: 2129 return; 2130 } 2131 2132 if ((offset + sizeof(u_int16_t)) > m->m_len) 2133 m_copyback(m, offset, sizeof(csum), &csum, M_NOWAIT); 2134 else 2135 *(u_int16_t *)(mtod(m, caddr_t) + offset) = csum; 2136 } 2137 2138 void 2139 in_proto_cksum_out(struct mbuf *m, struct ifnet *ifp) 2140 { 2141 if (m->m_pkthdr.csum_flags & M_TCP_CSUM_OUT) { 2142 if (!ifp || !(ifp->if_capabilities & IFCAP_CSUM_TCPv4) || 2143 ifp->if_bridge != NULL) { 2144 in_delayed_cksum(m); 2145 m->m_pkthdr.csum_flags &= ~M_TCP_CSUM_OUT; /* Clear */ 2146 } 2147 } else if (m->m_pkthdr.csum_flags & M_UDP_CSUM_OUT) { 2148 if (!ifp || !(ifp->if_capabilities & IFCAP_CSUM_UDPv4) || 2149 ifp->if_bridge != NULL) { 2150 in_delayed_cksum(m); 2151 m->m_pkthdr.csum_flags &= ~M_UDP_CSUM_OUT; /* Clear */ 2152 } 2153 } else if (m->m_pkthdr.csum_flags & M_ICMP_CSUM_OUT) { 2154 struct ip *ip = mtod(m, struct ip *); 2155 int hlen; 2156 struct icmp *icp; 2157 2158 hlen = ip->ip_hl << 2; 2159 m->m_data += hlen; 2160 m->m_len -= hlen; 2161 icp = mtod(m, struct icmp *); 2162 icp->icmp_cksum = 0; 2163 icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen); 2164 m->m_data -= hlen; 2165 m->m_len += hlen; 2166 m->m_pkthdr.csum_flags &= ~M_ICMP_CSUM_OUT; /* Clear */ 2167 } 2168 } 2169