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