1 /* $NetBSD: ip_output.c,v 1.88 2001/09/17 17:27:00 thorpej Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1998 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Public Access Networks Corporation ("Panix"). It was developed under 38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed by the NetBSD 51 * Foundation, Inc. and its contributors. 52 * 4. Neither the name of The NetBSD Foundation nor the names of its 53 * contributors may be used to endorse or promote products derived 54 * from this software without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66 * POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69 /* 70 * Copyright (c) 1982, 1986, 1988, 1990, 1993 71 * The Regents of the University of California. All rights reserved. 72 * 73 * Redistribution and use in source and binary forms, with or without 74 * modification, are permitted provided that the following conditions 75 * are met: 76 * 1. Redistributions of source code must retain the above copyright 77 * notice, this list of conditions and the following disclaimer. 78 * 2. Redistributions in binary form must reproduce the above copyright 79 * notice, this list of conditions and the following disclaimer in the 80 * documentation and/or other materials provided with the distribution. 81 * 3. All advertising materials mentioning features or use of this software 82 * must display the following acknowledgement: 83 * This product includes software developed by the University of 84 * California, Berkeley and its contributors. 85 * 4. Neither the name of the University nor the names of its contributors 86 * may be used to endorse or promote products derived from this software 87 * without specific prior written permission. 88 * 89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 99 * SUCH DAMAGE. 100 * 101 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 102 */ 103 104 #include "opt_pfil_hooks.h" 105 #include "opt_ipsec.h" 106 #include "opt_mrouting.h" 107 108 #include <sys/param.h> 109 #include <sys/malloc.h> 110 #include <sys/mbuf.h> 111 #include <sys/errno.h> 112 #include <sys/protosw.h> 113 #include <sys/socket.h> 114 #include <sys/socketvar.h> 115 #include <sys/systm.h> 116 #include <sys/proc.h> 117 118 #include <net/if.h> 119 #include <net/route.h> 120 #include <net/pfil.h> 121 122 #include <netinet/in.h> 123 #include <netinet/in_systm.h> 124 #include <netinet/ip.h> 125 #include <netinet/in_pcb.h> 126 #include <netinet/in_var.h> 127 #include <netinet/ip_var.h> 128 129 #ifdef MROUTING 130 #include <netinet/ip_mroute.h> 131 #endif 132 133 #include <machine/stdarg.h> 134 135 #ifdef IPSEC 136 #include <netinet6/ipsec.h> 137 #include <netkey/key.h> 138 #include <netkey/key_debug.h> 139 #endif /*IPSEC*/ 140 141 static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *)); 142 static struct ifnet *ip_multicast_if __P((struct in_addr *, int *)); 143 static void ip_mloopback 144 __P((struct ifnet *, struct mbuf *, struct sockaddr_in *)); 145 146 #ifdef PFIL_HOOKS 147 extern struct pfil_head inet_pfil_hook; /* XXX */ 148 #endif 149 150 /* 151 * IP output. The packet in mbuf chain m contains a skeletal IP 152 * header (with len, off, ttl, proto, tos, src, dst). 153 * The mbuf chain containing the packet will be freed. 154 * The mbuf opt, if present, will not be freed. 155 */ 156 int 157 #if __STDC__ 158 ip_output(struct mbuf *m0, ...) 159 #else 160 ip_output(m0, va_alist) 161 struct mbuf *m0; 162 va_dcl 163 #endif 164 { 165 struct ip *ip, *mhip; 166 struct ifnet *ifp; 167 struct mbuf *m = m0; 168 int hlen = sizeof (struct ip); 169 int len, off, error = 0; 170 struct route iproute; 171 struct sockaddr_in *dst; 172 struct in_ifaddr *ia; 173 struct mbuf *opt; 174 struct route *ro; 175 int flags, sw_csum; 176 int *mtu_p; 177 int mtu; 178 struct ip_moptions *imo; 179 va_list ap; 180 #ifdef IPSEC 181 struct socket *so; 182 struct secpolicy *sp = NULL; 183 #endif /*IPSEC*/ 184 u_int16_t ip_len; 185 186 va_start(ap, m0); 187 opt = va_arg(ap, struct mbuf *); 188 ro = va_arg(ap, struct route *); 189 flags = va_arg(ap, int); 190 imo = va_arg(ap, struct ip_moptions *); 191 if (flags & IP_RETURNMTU) 192 mtu_p = va_arg(ap, int *); 193 else 194 mtu_p = NULL; 195 va_end(ap); 196 197 #ifdef IPSEC 198 so = ipsec_getsocket(m); 199 (void)ipsec_setsocket(m, NULL); 200 #endif /*IPSEC*/ 201 202 #ifdef DIAGNOSTIC 203 if ((m->m_flags & M_PKTHDR) == 0) 204 panic("ip_output no HDR"); 205 #endif 206 if (opt) { 207 m = ip_insertoptions(m, opt, &len); 208 hlen = len; 209 } 210 ip = mtod(m, struct ip *); 211 /* 212 * Fill in IP header. 213 */ 214 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 215 ip->ip_v = IPVERSION; 216 ip->ip_off = 0; 217 ip->ip_id = htons(ip_id++); 218 ip->ip_hl = hlen >> 2; 219 ipstat.ips_localout++; 220 } else { 221 hlen = ip->ip_hl << 2; 222 } 223 /* 224 * Route packet. 225 */ 226 if (ro == 0) { 227 ro = &iproute; 228 bzero((caddr_t)ro, sizeof (*ro)); 229 } 230 dst = satosin(&ro->ro_dst); 231 /* 232 * If there is a cached route, 233 * check that it is to the same destination 234 * and is still up. If not, free it and try again. 235 */ 236 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 237 !in_hosteq(dst->sin_addr, ip->ip_dst))) { 238 RTFREE(ro->ro_rt); 239 ro->ro_rt = (struct rtentry *)0; 240 } 241 if (ro->ro_rt == 0) { 242 dst->sin_family = AF_INET; 243 dst->sin_len = sizeof(*dst); 244 dst->sin_addr = ip->ip_dst; 245 } 246 /* 247 * If routing to interface only, 248 * short circuit routing lookup. 249 */ 250 if (flags & IP_ROUTETOIF) { 251 if ((ia = ifatoia(ifa_ifwithladdr(sintosa(dst)))) == 0) { 252 ipstat.ips_noroute++; 253 error = ENETUNREACH; 254 goto bad; 255 } 256 ifp = ia->ia_ifp; 257 mtu = ifp->if_mtu; 258 ip->ip_ttl = 1; 259 } else { 260 if (ro->ro_rt == 0) 261 rtalloc(ro); 262 if (ro->ro_rt == 0) { 263 ipstat.ips_noroute++; 264 error = EHOSTUNREACH; 265 goto bad; 266 } 267 ia = ifatoia(ro->ro_rt->rt_ifa); 268 ifp = ro->ro_rt->rt_ifp; 269 if ((mtu = ro->ro_rt->rt_rmx.rmx_mtu) == 0) 270 mtu = ifp->if_mtu; 271 ro->ro_rt->rt_use++; 272 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 273 dst = satosin(ro->ro_rt->rt_gateway); 274 } 275 if (IN_MULTICAST(ip->ip_dst.s_addr) || 276 (ip->ip_dst.s_addr == INADDR_BROADCAST)) { 277 struct in_multi *inm; 278 279 m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ? 280 M_BCAST : M_MCAST; 281 /* 282 * IP destination address is multicast. Make sure "dst" 283 * still points to the address in "ro". (It may have been 284 * changed to point to a gateway address, above.) 285 */ 286 dst = satosin(&ro->ro_dst); 287 /* 288 * See if the caller provided any multicast options 289 */ 290 if (imo != NULL) { 291 ip->ip_ttl = imo->imo_multicast_ttl; 292 if (imo->imo_multicast_ifp != NULL) { 293 ifp = imo->imo_multicast_ifp; 294 mtu = ifp->if_mtu; 295 } 296 } else 297 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 298 /* 299 * Confirm that the outgoing interface supports multicast. 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)) { 305 ipstat.ips_noroute++; 306 error = ENETUNREACH; 307 goto bad; 308 } 309 /* 310 * If source address not specified yet, use an address 311 * of outgoing interface. 312 */ 313 if (in_nullhost(ip->ip_src)) { 314 struct in_ifaddr *ia; 315 316 IFP_TO_IA(ifp, ia); 317 ip->ip_src = ia->ia_addr.sin_addr; 318 } 319 320 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 321 if (inm != NULL && 322 (imo == NULL || imo->imo_multicast_loop)) { 323 /* 324 * If we belong to the destination multicast group 325 * on the outgoing interface, and the caller did not 326 * forbid loopback, loop back a copy. 327 */ 328 ip_mloopback(ifp, m, dst); 329 } 330 #ifdef MROUTING 331 else { 332 /* 333 * If we are acting as a multicast router, perform 334 * multicast forwarding as if the packet had just 335 * arrived on the interface to which we are about 336 * to send. The multicast forwarding function 337 * recursively calls this function, using the 338 * IP_FORWARDING flag to prevent infinite recursion. 339 * 340 * Multicasts that are looped back by ip_mloopback(), 341 * above, will be forwarded by the ip_input() routine, 342 * if necessary. 343 */ 344 extern struct socket *ip_mrouter; 345 346 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 347 if (ip_mforward(m, ifp) != 0) { 348 m_freem(m); 349 goto done; 350 } 351 } 352 } 353 #endif 354 /* 355 * Multicasts with a time-to-live of zero may be looped- 356 * back, above, but must not be transmitted on a network. 357 * Also, multicasts addressed to the loopback interface 358 * are not sent -- the above call to ip_mloopback() will 359 * loop back a copy if this host actually belongs to the 360 * destination group on the loopback interface. 361 */ 362 if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) { 363 m_freem(m); 364 goto done; 365 } 366 367 goto sendit; 368 } 369 #ifndef notdef 370 /* 371 * If source address not specified yet, use address 372 * of outgoing interface. 373 */ 374 if (in_nullhost(ip->ip_src)) 375 ip->ip_src = ia->ia_addr.sin_addr; 376 #endif 377 378 /* 379 * packets with Class-D address as source are not valid per 380 * RFC 1112 381 */ 382 if (IN_MULTICAST(ip->ip_src.s_addr)) { 383 ipstat.ips_odropped++; 384 error = EADDRNOTAVAIL; 385 goto bad; 386 } 387 388 /* 389 * Look for broadcast address and 390 * and verify user is allowed to send 391 * such a packet. 392 */ 393 if (in_broadcast(dst->sin_addr, ifp)) { 394 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 395 error = EADDRNOTAVAIL; 396 goto bad; 397 } 398 if ((flags & IP_ALLOWBROADCAST) == 0) { 399 error = EACCES; 400 goto bad; 401 } 402 /* don't allow broadcast messages to be fragmented */ 403 if ((u_int16_t)ip->ip_len > ifp->if_mtu) { 404 error = EMSGSIZE; 405 goto bad; 406 } 407 m->m_flags |= M_BCAST; 408 } else 409 m->m_flags &= ~M_BCAST; 410 411 sendit: 412 /* 413 * If we're doing Path MTU Discovery, we need to set DF unless 414 * the route's MTU is locked. 415 */ 416 if ((flags & IP_MTUDISC) != 0 && ro->ro_rt != NULL && 417 (ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0) 418 ip->ip_off |= IP_DF; 419 420 /* 421 * Remember the current ip_len and ip_off, and swap them into 422 * network order. 423 */ 424 ip_len = ip->ip_len; 425 426 HTONS(ip->ip_len); 427 HTONS(ip->ip_off); 428 429 #ifdef IPSEC 430 /* get SP for this packet */ 431 if (so == NULL) 432 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error); 433 else 434 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 435 436 if (sp == NULL) { 437 ipsecstat.out_inval++; 438 goto bad; 439 } 440 441 error = 0; 442 443 /* check policy */ 444 switch (sp->policy) { 445 case IPSEC_POLICY_DISCARD: 446 /* 447 * This packet is just discarded. 448 */ 449 ipsecstat.out_polvio++; 450 goto bad; 451 452 case IPSEC_POLICY_BYPASS: 453 case IPSEC_POLICY_NONE: 454 /* no need to do IPsec. */ 455 goto skip_ipsec; 456 457 case IPSEC_POLICY_IPSEC: 458 if (sp->req == NULL) { 459 /* XXX should be panic ? */ 460 printf("ip_output: No IPsec request specified.\n"); 461 error = EINVAL; 462 goto bad; 463 } 464 break; 465 466 case IPSEC_POLICY_ENTRUST: 467 default: 468 printf("ip_output: Invalid policy found. %d\n", sp->policy); 469 } 470 471 /* 472 * ipsec4_output() expects ip_len and ip_off in network 473 * order. They have been set to network order above. 474 */ 475 476 { 477 struct ipsec_output_state state; 478 bzero(&state, sizeof(state)); 479 state.m = m; 480 if (flags & IP_ROUTETOIF) { 481 state.ro = &iproute; 482 bzero(&iproute, sizeof(iproute)); 483 } else 484 state.ro = ro; 485 state.dst = (struct sockaddr *)dst; 486 487 /* 488 * We can't defer the checksum of payload data if 489 * we're about to encrypt/authenticate it. 490 * 491 * XXX When we support crypto offloading functions of 492 * XXX network interfaces, we need to reconsider this, 493 * XXX since it's likely that they'll support checksumming, 494 * XXX as well. 495 */ 496 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 497 in_delayed_cksum(m); 498 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 499 } 500 501 error = ipsec4_output(&state, sp, flags); 502 503 m = state.m; 504 if (flags & IP_ROUTETOIF) { 505 /* 506 * if we have tunnel mode SA, we may need to ignore 507 * IP_ROUTETOIF. 508 */ 509 if (state.ro != &iproute || state.ro->ro_rt != NULL) { 510 flags &= ~IP_ROUTETOIF; 511 ro = state.ro; 512 } 513 } else 514 ro = state.ro; 515 dst = (struct sockaddr_in *)state.dst; 516 if (error) { 517 /* mbuf is already reclaimed in ipsec4_output. */ 518 m0 = NULL; 519 switch (error) { 520 case EHOSTUNREACH: 521 case ENETUNREACH: 522 case EMSGSIZE: 523 case ENOBUFS: 524 case ENOMEM: 525 break; 526 default: 527 printf("ip4_output (ipsec): error code %d\n", error); 528 /*fall through*/ 529 case ENOENT: 530 /* don't show these error codes to the user */ 531 error = 0; 532 break; 533 } 534 goto bad; 535 } 536 } 537 538 /* be sure to update variables that are affected by ipsec4_output() */ 539 ip = mtod(m, struct ip *); 540 #ifdef _IP_VHL 541 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 542 #else 543 hlen = ip->ip_hl << 2; 544 #endif 545 ip_len = ntohs(ip->ip_len); 546 547 if (ro->ro_rt == NULL) { 548 if ((flags & IP_ROUTETOIF) == 0) { 549 printf("ip_output: " 550 "can't update route after IPsec processing\n"); 551 error = EHOSTUNREACH; /*XXX*/ 552 goto bad; 553 } 554 } else { 555 /* nobody uses ia beyond here */ 556 ifp = ro->ro_rt->rt_ifp; 557 } 558 559 skip_ipsec: 560 #endif /*IPSEC*/ 561 562 #ifdef PFIL_HOOKS 563 /* 564 * Run through list of hooks for output packets. 565 */ 566 if ((error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, 567 PFIL_OUT)) != 0) 568 goto done; 569 if (m == NULL) 570 goto done; 571 572 ip = mtod(m, struct ip *); 573 #endif /* PFIL_HOOKS */ 574 575 /* 576 * If small enough for mtu of path, can just send directly. 577 */ 578 if (ip_len <= mtu) { 579 #if IFA_STATS 580 /* 581 * search for the source address structure to 582 * maintain output statistics. 583 */ 584 INADDR_TO_IA(ip->ip_src, ia); 585 if (ia) 586 ia->ia_ifa.ifa_data.ifad_outbytes += ip_len; 587 #endif 588 /* 589 * Always initialize the sum to 0! Some HW assisted 590 * checksumming requires this. 591 */ 592 ip->ip_sum = 0; 593 m->m_pkthdr.csum_flags |= M_CSUM_IPv4; 594 595 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx; 596 597 /* 598 * Perform any checksums that the hardware can't do 599 * for us. 600 * 601 * XXX Does any hardware require the {th,uh}_sum 602 * XXX fields to be 0? 603 */ 604 if (sw_csum & M_CSUM_IPv4) 605 ip->ip_sum = in_cksum(m, hlen); 606 if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 607 in_delayed_cksum(m); 608 sw_csum &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 609 } 610 m->m_pkthdr.csum_flags &= ifp->if_csum_flags_tx; 611 612 #ifdef IPSEC 613 /* clean ipsec history once it goes out of the node */ 614 ipsec_delaux(m); 615 #endif 616 error = (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt); 617 goto done; 618 } 619 620 /* 621 * We can't use HW checksumming if we're about to 622 * to fragment the packet. 623 * 624 * XXX Some hardware can do this. 625 */ 626 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 627 in_delayed_cksum(m); 628 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 629 } 630 631 /* 632 * Too large for interface; fragment if possible. 633 * Must be able to put at least 8 bytes per fragment. 634 * 635 * Note we swap ip_len and ip_off into host order to make 636 * the logic below a little simpler. 637 */ 638 639 NTOHS(ip->ip_len); 640 NTOHS(ip->ip_off); 641 642 if (ip->ip_off & IP_DF) { 643 if (flags & IP_RETURNMTU) 644 *mtu_p = mtu; 645 error = EMSGSIZE; 646 ipstat.ips_cantfrag++; 647 goto bad; 648 } 649 len = (mtu - hlen) &~ 7; 650 if (len < 8) { 651 error = EMSGSIZE; 652 goto bad; 653 } 654 655 { 656 int mhlen, firstlen = len; 657 struct mbuf **mnext = &m->m_nextpkt; 658 int fragments = 0; 659 int s; 660 661 /* 662 * Loop through length of segment after first fragment, 663 * make new header and copy data of each part and link onto chain. 664 */ 665 m0 = m; 666 mhlen = sizeof (struct ip); 667 for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len) { 668 MGETHDR(m, M_DONTWAIT, MT_HEADER); 669 if (m == 0) { 670 error = ENOBUFS; 671 ipstat.ips_odropped++; 672 goto sendorfree; 673 } 674 *mnext = m; 675 mnext = &m->m_nextpkt; 676 m->m_data += max_linkhdr; 677 mhip = mtod(m, struct ip *); 678 *mhip = *ip; 679 /* we must inherit MCAST and BCAST flags */ 680 m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST); 681 if (hlen > sizeof (struct ip)) { 682 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 683 mhip->ip_hl = mhlen >> 2; 684 } 685 m->m_len = mhlen; 686 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF); 687 if (ip->ip_off & IP_MF) 688 mhip->ip_off |= IP_MF; 689 if (off + len >= (u_int16_t)ip->ip_len) 690 len = (u_int16_t)ip->ip_len - off; 691 else 692 mhip->ip_off |= IP_MF; 693 mhip->ip_len = htons((u_int16_t)(len + mhlen)); 694 m->m_next = m_copy(m0, off, len); 695 if (m->m_next == 0) { 696 error = ENOBUFS; /* ??? */ 697 ipstat.ips_odropped++; 698 goto sendorfree; 699 } 700 m->m_pkthdr.len = mhlen + len; 701 m->m_pkthdr.rcvif = (struct ifnet *)0; 702 HTONS(mhip->ip_off); 703 mhip->ip_sum = 0; 704 mhip->ip_sum = in_cksum(m, mhlen); 705 ipstat.ips_ofragments++; 706 fragments++; 707 } 708 /* 709 * Update first fragment by trimming what's been copied out 710 * and updating header, then send each fragment (in order). 711 */ 712 m = m0; 713 m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len); 714 m->m_pkthdr.len = hlen + firstlen; 715 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len); 716 ip->ip_off |= IP_MF; 717 HTONS(ip->ip_off); 718 ip->ip_sum = 0; 719 ip->ip_sum = in_cksum(m, hlen); 720 sendorfree: 721 /* 722 * If there is no room for all the fragments, don't queue 723 * any of them. 724 */ 725 s = splnet(); 726 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments) 727 error = ENOBUFS; 728 splx(s); 729 for (m = m0; m; m = m0) { 730 m0 = m->m_nextpkt; 731 m->m_nextpkt = 0; 732 if (error == 0) { 733 #if IFA_STATS 734 /* 735 * search for the source address structure to 736 * maintain output statistics. 737 */ 738 INADDR_TO_IA(ip->ip_src, ia); 739 if (ia) { 740 ia->ia_ifa.ifa_data.ifad_outbytes += 741 ntohs(ip->ip_len); 742 } 743 #endif 744 #ifdef IPSEC 745 /* clean ipsec history once it goes out of the node */ 746 ipsec_delaux(m); 747 #endif 748 error = (*ifp->if_output)(ifp, m, sintosa(dst), 749 ro->ro_rt); 750 } else 751 m_freem(m); 752 } 753 754 if (error == 0) 755 ipstat.ips_fragmented++; 756 } 757 done: 758 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) { 759 RTFREE(ro->ro_rt); 760 ro->ro_rt = 0; 761 } 762 763 #ifdef IPSEC 764 if (sp != NULL) { 765 KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 766 printf("DP ip_output call free SP:%p\n", sp)); 767 key_freesp(sp); 768 } 769 #endif /* IPSEC */ 770 771 return (error); 772 bad: 773 m_freem(m); 774 goto done; 775 } 776 777 /* 778 * Process a delayed payload checksum calculation. 779 */ 780 void 781 in_delayed_cksum(struct mbuf *m) 782 { 783 struct ip *ip; 784 u_int16_t csum, offset; 785 786 ip = mtod(m, struct ip *); 787 offset = ip->ip_hl << 2; 788 csum = in4_cksum(m, 0, offset, ntohs(ip->ip_len) - offset); 789 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv4) != 0) 790 csum = 0xffff; 791 792 offset += m->m_pkthdr.csum_data; /* checksum offset */ 793 794 if ((offset + sizeof(u_int16_t)) > m->m_len) { 795 /* This happen when ip options were inserted 796 printf("in_delayed_cksum: pullup len %d off %d proto %d\n", 797 m->m_len, offset, ip->ip_p); 798 */ 799 m_copyback(m, offset, sizeof(csum), (caddr_t) &csum); 800 } else 801 *(u_int16_t *)(mtod(m, caddr_t) + offset) = csum; 802 } 803 804 /* 805 * Determine the maximum length of the options to be inserted; 806 * we would far rather allocate too much space rather than too little. 807 */ 808 809 u_int 810 ip_optlen(inp) 811 struct inpcb *inp; 812 { 813 struct mbuf *m = inp->inp_options; 814 815 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst)) 816 return(m->m_len - offsetof(struct ipoption, ipopt_dst)); 817 else 818 return 0; 819 } 820 821 822 /* 823 * Insert IP options into preformed packet. 824 * Adjust IP destination as required for IP source routing, 825 * as indicated by a non-zero in_addr at the start of the options. 826 */ 827 static struct mbuf * 828 ip_insertoptions(m, opt, phlen) 829 struct mbuf *m; 830 struct mbuf *opt; 831 int *phlen; 832 { 833 struct ipoption *p = mtod(opt, struct ipoption *); 834 struct mbuf *n; 835 struct ip *ip = mtod(m, struct ip *); 836 unsigned optlen; 837 838 optlen = opt->m_len - sizeof(p->ipopt_dst); 839 if (optlen + (u_int16_t)ip->ip_len > IP_MAXPACKET) 840 return (m); /* XXX should fail */ 841 if (!in_nullhost(p->ipopt_dst)) 842 ip->ip_dst = p->ipopt_dst; 843 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 844 MGETHDR(n, M_DONTWAIT, MT_HEADER); 845 if (n == 0) 846 return (m); 847 M_COPY_PKTHDR(n, m); 848 m->m_flags &= ~M_PKTHDR; 849 m->m_len -= sizeof(struct ip); 850 m->m_data += sizeof(struct ip); 851 n->m_next = m; 852 m = n; 853 m->m_len = optlen + sizeof(struct ip); 854 m->m_data += max_linkhdr; 855 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 856 } else { 857 m->m_data -= optlen; 858 m->m_len += optlen; 859 memmove(mtod(m, caddr_t), ip, sizeof(struct ip)); 860 } 861 m->m_pkthdr.len += optlen; 862 ip = mtod(m, struct ip *); 863 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen); 864 *phlen = sizeof(struct ip) + optlen; 865 ip->ip_len += optlen; 866 return (m); 867 } 868 869 /* 870 * Copy options from ip to jp, 871 * omitting those not copied during fragmentation. 872 */ 873 int 874 ip_optcopy(ip, jp) 875 struct ip *ip, *jp; 876 { 877 u_char *cp, *dp; 878 int opt, optlen, cnt; 879 880 cp = (u_char *)(ip + 1); 881 dp = (u_char *)(jp + 1); 882 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 883 for (; cnt > 0; cnt -= optlen, cp += optlen) { 884 opt = cp[0]; 885 if (opt == IPOPT_EOL) 886 break; 887 if (opt == IPOPT_NOP) { 888 /* Preserve for IP mcast tunnel's LSRR alignment. */ 889 *dp++ = IPOPT_NOP; 890 optlen = 1; 891 continue; 892 } 893 #ifdef DIAGNOSTIC 894 if (cnt < IPOPT_OLEN + sizeof(*cp)) 895 panic("malformed IPv4 option passed to ip_optcopy"); 896 #endif 897 optlen = cp[IPOPT_OLEN]; 898 #ifdef DIAGNOSTIC 899 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 900 panic("malformed IPv4 option passed to ip_optcopy"); 901 #endif 902 /* bogus lengths should have been caught by ip_dooptions */ 903 if (optlen > cnt) 904 optlen = cnt; 905 if (IPOPT_COPIED(opt)) { 906 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); 907 dp += optlen; 908 } 909 } 910 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 911 *dp++ = IPOPT_EOL; 912 return (optlen); 913 } 914 915 /* 916 * IP socket option processing. 917 */ 918 int 919 ip_ctloutput(op, so, level, optname, mp) 920 int op; 921 struct socket *so; 922 int level, optname; 923 struct mbuf **mp; 924 { 925 struct inpcb *inp = sotoinpcb(so); 926 struct mbuf *m = *mp; 927 int optval = 0; 928 int error = 0; 929 #ifdef IPSEC 930 #ifdef __NetBSD__ 931 struct proc *p = curproc; /*XXX*/ 932 #endif 933 #endif 934 935 if (level != IPPROTO_IP) { 936 error = EINVAL; 937 if (op == PRCO_SETOPT && *mp) 938 (void) m_free(*mp); 939 } else switch (op) { 940 941 case PRCO_SETOPT: 942 switch (optname) { 943 case IP_OPTIONS: 944 #ifdef notyet 945 case IP_RETOPTS: 946 return (ip_pcbopts(optname, &inp->inp_options, m)); 947 #else 948 return (ip_pcbopts(&inp->inp_options, m)); 949 #endif 950 951 case IP_TOS: 952 case IP_TTL: 953 case IP_RECVOPTS: 954 case IP_RECVRETOPTS: 955 case IP_RECVDSTADDR: 956 case IP_RECVIF: 957 if (m == NULL || m->m_len != sizeof(int)) 958 error = EINVAL; 959 else { 960 optval = *mtod(m, int *); 961 switch (optname) { 962 963 case IP_TOS: 964 inp->inp_ip.ip_tos = optval; 965 break; 966 967 case IP_TTL: 968 inp->inp_ip.ip_ttl = optval; 969 break; 970 #define OPTSET(bit) \ 971 if (optval) \ 972 inp->inp_flags |= bit; \ 973 else \ 974 inp->inp_flags &= ~bit; 975 976 case IP_RECVOPTS: 977 OPTSET(INP_RECVOPTS); 978 break; 979 980 case IP_RECVRETOPTS: 981 OPTSET(INP_RECVRETOPTS); 982 break; 983 984 case IP_RECVDSTADDR: 985 OPTSET(INP_RECVDSTADDR); 986 break; 987 988 case IP_RECVIF: 989 OPTSET(INP_RECVIF); 990 break; 991 } 992 } 993 break; 994 #undef OPTSET 995 996 case IP_MULTICAST_IF: 997 case IP_MULTICAST_TTL: 998 case IP_MULTICAST_LOOP: 999 case IP_ADD_MEMBERSHIP: 1000 case IP_DROP_MEMBERSHIP: 1001 error = ip_setmoptions(optname, &inp->inp_moptions, m); 1002 break; 1003 1004 case IP_PORTRANGE: 1005 if (m == 0 || m->m_len != sizeof(int)) 1006 error = EINVAL; 1007 else { 1008 optval = *mtod(m, int *); 1009 1010 switch (optval) { 1011 1012 case IP_PORTRANGE_DEFAULT: 1013 case IP_PORTRANGE_HIGH: 1014 inp->inp_flags &= ~(INP_LOWPORT); 1015 break; 1016 1017 case IP_PORTRANGE_LOW: 1018 inp->inp_flags |= INP_LOWPORT; 1019 break; 1020 1021 default: 1022 error = EINVAL; 1023 break; 1024 } 1025 } 1026 break; 1027 1028 #ifdef IPSEC 1029 case IP_IPSEC_POLICY: 1030 { 1031 caddr_t req = NULL; 1032 size_t len = 0; 1033 int priv = 0; 1034 1035 #ifdef __NetBSD__ 1036 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) 1037 priv = 0; 1038 else 1039 priv = 1; 1040 #else 1041 priv = (in6p->in6p_socket->so_state & SS_PRIV); 1042 #endif 1043 if (m) { 1044 req = mtod(m, caddr_t); 1045 len = m->m_len; 1046 } 1047 error = ipsec4_set_policy(inp, optname, req, len, priv); 1048 break; 1049 } 1050 #endif /*IPSEC*/ 1051 1052 default: 1053 error = ENOPROTOOPT; 1054 break; 1055 } 1056 if (m) 1057 (void)m_free(m); 1058 break; 1059 1060 case PRCO_GETOPT: 1061 switch (optname) { 1062 case IP_OPTIONS: 1063 case IP_RETOPTS: 1064 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1065 if (inp->inp_options) { 1066 m->m_len = inp->inp_options->m_len; 1067 bcopy(mtod(inp->inp_options, caddr_t), 1068 mtod(m, caddr_t), (unsigned)m->m_len); 1069 } else 1070 m->m_len = 0; 1071 break; 1072 1073 case IP_TOS: 1074 case IP_TTL: 1075 case IP_RECVOPTS: 1076 case IP_RECVRETOPTS: 1077 case IP_RECVDSTADDR: 1078 case IP_RECVIF: 1079 case IP_ERRORMTU: 1080 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1081 m->m_len = sizeof(int); 1082 switch (optname) { 1083 1084 case IP_TOS: 1085 optval = inp->inp_ip.ip_tos; 1086 break; 1087 1088 case IP_TTL: 1089 optval = inp->inp_ip.ip_ttl; 1090 break; 1091 1092 case IP_ERRORMTU: 1093 optval = inp->inp_errormtu; 1094 break; 1095 1096 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1097 1098 case IP_RECVOPTS: 1099 optval = OPTBIT(INP_RECVOPTS); 1100 break; 1101 1102 case IP_RECVRETOPTS: 1103 optval = OPTBIT(INP_RECVRETOPTS); 1104 break; 1105 1106 case IP_RECVDSTADDR: 1107 optval = OPTBIT(INP_RECVDSTADDR); 1108 break; 1109 1110 case IP_RECVIF: 1111 optval = OPTBIT(INP_RECVIF); 1112 break; 1113 } 1114 *mtod(m, int *) = optval; 1115 break; 1116 1117 #ifdef IPSEC 1118 case IP_IPSEC_POLICY: 1119 { 1120 caddr_t req = NULL; 1121 size_t len = 0; 1122 1123 if (m) { 1124 req = mtod(m, caddr_t); 1125 len = m->m_len; 1126 } 1127 error = ipsec4_get_policy(inp, req, len, mp); 1128 break; 1129 } 1130 #endif /*IPSEC*/ 1131 1132 case IP_MULTICAST_IF: 1133 case IP_MULTICAST_TTL: 1134 case IP_MULTICAST_LOOP: 1135 case IP_ADD_MEMBERSHIP: 1136 case IP_DROP_MEMBERSHIP: 1137 error = ip_getmoptions(optname, inp->inp_moptions, mp); 1138 break; 1139 1140 case IP_PORTRANGE: 1141 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1142 m->m_len = sizeof(int); 1143 1144 if (inp->inp_flags & INP_LOWPORT) 1145 optval = IP_PORTRANGE_LOW; 1146 else 1147 optval = IP_PORTRANGE_DEFAULT; 1148 1149 *mtod(m, int *) = optval; 1150 break; 1151 1152 default: 1153 error = ENOPROTOOPT; 1154 break; 1155 } 1156 break; 1157 } 1158 return (error); 1159 } 1160 1161 /* 1162 * Set up IP options in pcb for insertion in output packets. 1163 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1164 * with destination address if source routed. 1165 */ 1166 int 1167 #ifdef notyet 1168 ip_pcbopts(optname, pcbopt, m) 1169 int optname; 1170 #else 1171 ip_pcbopts(pcbopt, m) 1172 #endif 1173 struct mbuf **pcbopt; 1174 struct mbuf *m; 1175 { 1176 int cnt, optlen; 1177 u_char *cp; 1178 u_char opt; 1179 1180 /* turn off any old options */ 1181 if (*pcbopt) 1182 (void)m_free(*pcbopt); 1183 *pcbopt = 0; 1184 if (m == (struct mbuf *)0 || m->m_len == 0) { 1185 /* 1186 * Only turning off any previous options. 1187 */ 1188 if (m) 1189 (void)m_free(m); 1190 return (0); 1191 } 1192 1193 #ifndef __vax__ 1194 if (m->m_len % sizeof(int32_t)) 1195 goto bad; 1196 #endif 1197 /* 1198 * IP first-hop destination address will be stored before 1199 * actual options; move other options back 1200 * and clear it when none present. 1201 */ 1202 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1203 goto bad; 1204 cnt = m->m_len; 1205 m->m_len += sizeof(struct in_addr); 1206 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1207 memmove(cp, mtod(m, caddr_t), (unsigned)cnt); 1208 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 1209 1210 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1211 opt = cp[IPOPT_OPTVAL]; 1212 if (opt == IPOPT_EOL) 1213 break; 1214 if (opt == IPOPT_NOP) 1215 optlen = 1; 1216 else { 1217 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1218 goto bad; 1219 optlen = cp[IPOPT_OLEN]; 1220 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1221 goto bad; 1222 } 1223 switch (opt) { 1224 1225 default: 1226 break; 1227 1228 case IPOPT_LSRR: 1229 case IPOPT_SSRR: 1230 /* 1231 * user process specifies route as: 1232 * ->A->B->C->D 1233 * D must be our final destination (but we can't 1234 * check that since we may not have connected yet). 1235 * A is first hop destination, which doesn't appear in 1236 * actual IP option, but is stored before the options. 1237 */ 1238 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1239 goto bad; 1240 m->m_len -= sizeof(struct in_addr); 1241 cnt -= sizeof(struct in_addr); 1242 optlen -= sizeof(struct in_addr); 1243 cp[IPOPT_OLEN] = optlen; 1244 /* 1245 * Move first hop before start of options. 1246 */ 1247 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1248 sizeof(struct in_addr)); 1249 /* 1250 * Then copy rest of options back 1251 * to close up the deleted entry. 1252 */ 1253 memmove(&cp[IPOPT_OFFSET+1], 1254 (caddr_t)(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)), 1255 (unsigned)cnt + sizeof(struct in_addr)); 1256 break; 1257 } 1258 } 1259 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1260 goto bad; 1261 *pcbopt = m; 1262 return (0); 1263 1264 bad: 1265 (void)m_free(m); 1266 return (EINVAL); 1267 } 1268 1269 /* 1270 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1271 */ 1272 static struct ifnet * 1273 ip_multicast_if(a, ifindexp) 1274 struct in_addr *a; 1275 int *ifindexp; 1276 { 1277 int ifindex; 1278 struct ifnet *ifp; 1279 1280 if (ifindexp) 1281 *ifindexp = 0; 1282 if (ntohl(a->s_addr) >> 24 == 0) { 1283 ifindex = ntohl(a->s_addr) & 0xffffff; 1284 if (ifindex < 0 || if_index < ifindex) 1285 return NULL; 1286 ifp = ifindex2ifnet[ifindex]; 1287 if (ifindexp) 1288 *ifindexp = ifindex; 1289 } else { 1290 INADDR_TO_IFP(*a, ifp); 1291 } 1292 return ifp; 1293 } 1294 1295 /* 1296 * Set the IP multicast options in response to user setsockopt(). 1297 */ 1298 int 1299 ip_setmoptions(optname, imop, m) 1300 int optname; 1301 struct ip_moptions **imop; 1302 struct mbuf *m; 1303 { 1304 int error = 0; 1305 u_char loop; 1306 int i; 1307 struct in_addr addr; 1308 struct ip_mreq *mreq; 1309 struct ifnet *ifp; 1310 struct ip_moptions *imo = *imop; 1311 struct route ro; 1312 struct sockaddr_in *dst; 1313 int ifindex; 1314 1315 if (imo == NULL) { 1316 /* 1317 * No multicast option buffer attached to the pcb; 1318 * allocate one and initialize to default values. 1319 */ 1320 imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS, 1321 M_WAITOK); 1322 1323 if (imo == NULL) 1324 return (ENOBUFS); 1325 *imop = imo; 1326 imo->imo_multicast_ifp = NULL; 1327 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1328 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1329 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1330 imo->imo_num_memberships = 0; 1331 } 1332 1333 switch (optname) { 1334 1335 case IP_MULTICAST_IF: 1336 /* 1337 * Select the interface for outgoing multicast packets. 1338 */ 1339 if (m == NULL || m->m_len != sizeof(struct in_addr)) { 1340 error = EINVAL; 1341 break; 1342 } 1343 addr = *(mtod(m, struct in_addr *)); 1344 /* 1345 * INADDR_ANY is used to remove a previous selection. 1346 * When no interface is selected, a default one is 1347 * chosen every time a multicast packet is sent. 1348 */ 1349 if (in_nullhost(addr)) { 1350 imo->imo_multicast_ifp = NULL; 1351 break; 1352 } 1353 /* 1354 * The selected interface is identified by its local 1355 * IP address. Find the interface and confirm that 1356 * it supports multicasting. 1357 */ 1358 ifp = ip_multicast_if(&addr, &ifindex); 1359 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1360 error = EADDRNOTAVAIL; 1361 break; 1362 } 1363 imo->imo_multicast_ifp = ifp; 1364 if (ifindex) 1365 imo->imo_multicast_addr = addr; 1366 else 1367 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1368 break; 1369 1370 case IP_MULTICAST_TTL: 1371 /* 1372 * Set the IP time-to-live for outgoing multicast packets. 1373 */ 1374 if (m == NULL || m->m_len != 1) { 1375 error = EINVAL; 1376 break; 1377 } 1378 imo->imo_multicast_ttl = *(mtod(m, u_char *)); 1379 break; 1380 1381 case IP_MULTICAST_LOOP: 1382 /* 1383 * Set the loopback flag for outgoing multicast packets. 1384 * Must be zero or one. 1385 */ 1386 if (m == NULL || m->m_len != 1 || 1387 (loop = *(mtod(m, u_char *))) > 1) { 1388 error = EINVAL; 1389 break; 1390 } 1391 imo->imo_multicast_loop = loop; 1392 break; 1393 1394 case IP_ADD_MEMBERSHIP: 1395 /* 1396 * Add a multicast group membership. 1397 * Group must be a valid IP multicast address. 1398 */ 1399 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 1400 error = EINVAL; 1401 break; 1402 } 1403 mreq = mtod(m, struct ip_mreq *); 1404 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) { 1405 error = EINVAL; 1406 break; 1407 } 1408 /* 1409 * If no interface address was provided, use the interface of 1410 * the route to the given multicast address. 1411 */ 1412 if (in_nullhost(mreq->imr_interface)) { 1413 bzero((caddr_t)&ro, sizeof(ro)); 1414 ro.ro_rt = NULL; 1415 dst = satosin(&ro.ro_dst); 1416 dst->sin_len = sizeof(*dst); 1417 dst->sin_family = AF_INET; 1418 dst->sin_addr = mreq->imr_multiaddr; 1419 rtalloc(&ro); 1420 if (ro.ro_rt == NULL) { 1421 error = EADDRNOTAVAIL; 1422 break; 1423 } 1424 ifp = ro.ro_rt->rt_ifp; 1425 rtfree(ro.ro_rt); 1426 } else { 1427 ifp = ip_multicast_if(&mreq->imr_interface, NULL); 1428 } 1429 /* 1430 * See if we found an interface, and confirm that it 1431 * supports multicast. 1432 */ 1433 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1434 error = EADDRNOTAVAIL; 1435 break; 1436 } 1437 /* 1438 * See if the membership already exists or if all the 1439 * membership slots are full. 1440 */ 1441 for (i = 0; i < imo->imo_num_memberships; ++i) { 1442 if (imo->imo_membership[i]->inm_ifp == ifp && 1443 in_hosteq(imo->imo_membership[i]->inm_addr, 1444 mreq->imr_multiaddr)) 1445 break; 1446 } 1447 if (i < imo->imo_num_memberships) { 1448 error = EADDRINUSE; 1449 break; 1450 } 1451 if (i == IP_MAX_MEMBERSHIPS) { 1452 error = ETOOMANYREFS; 1453 break; 1454 } 1455 /* 1456 * Everything looks good; add a new record to the multicast 1457 * address list for the given interface. 1458 */ 1459 if ((imo->imo_membership[i] = 1460 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) { 1461 error = ENOBUFS; 1462 break; 1463 } 1464 ++imo->imo_num_memberships; 1465 break; 1466 1467 case IP_DROP_MEMBERSHIP: 1468 /* 1469 * Drop a multicast group membership. 1470 * Group must be a valid IP multicast address. 1471 */ 1472 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 1473 error = EINVAL; 1474 break; 1475 } 1476 mreq = mtod(m, struct ip_mreq *); 1477 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) { 1478 error = EINVAL; 1479 break; 1480 } 1481 /* 1482 * If an interface address was specified, get a pointer 1483 * to its ifnet structure. 1484 */ 1485 if (in_nullhost(mreq->imr_interface)) 1486 ifp = NULL; 1487 else { 1488 ifp = ip_multicast_if(&mreq->imr_interface, NULL); 1489 if (ifp == NULL) { 1490 error = EADDRNOTAVAIL; 1491 break; 1492 } 1493 } 1494 /* 1495 * Find the membership in the membership array. 1496 */ 1497 for (i = 0; i < imo->imo_num_memberships; ++i) { 1498 if ((ifp == NULL || 1499 imo->imo_membership[i]->inm_ifp == ifp) && 1500 in_hosteq(imo->imo_membership[i]->inm_addr, 1501 mreq->imr_multiaddr)) 1502 break; 1503 } 1504 if (i == imo->imo_num_memberships) { 1505 error = EADDRNOTAVAIL; 1506 break; 1507 } 1508 /* 1509 * Give up the multicast address record to which the 1510 * membership points. 1511 */ 1512 in_delmulti(imo->imo_membership[i]); 1513 /* 1514 * Remove the gap in the membership array. 1515 */ 1516 for (++i; i < imo->imo_num_memberships; ++i) 1517 imo->imo_membership[i-1] = imo->imo_membership[i]; 1518 --imo->imo_num_memberships; 1519 break; 1520 1521 default: 1522 error = EOPNOTSUPP; 1523 break; 1524 } 1525 1526 /* 1527 * If all options have default values, no need to keep the mbuf. 1528 */ 1529 if (imo->imo_multicast_ifp == NULL && 1530 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1531 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1532 imo->imo_num_memberships == 0) { 1533 free(*imop, M_IPMOPTS); 1534 *imop = NULL; 1535 } 1536 1537 return (error); 1538 } 1539 1540 /* 1541 * Return the IP multicast options in response to user getsockopt(). 1542 */ 1543 int 1544 ip_getmoptions(optname, imo, mp) 1545 int optname; 1546 struct ip_moptions *imo; 1547 struct mbuf **mp; 1548 { 1549 u_char *ttl; 1550 u_char *loop; 1551 struct in_addr *addr; 1552 struct in_ifaddr *ia; 1553 1554 *mp = m_get(M_WAIT, MT_SOOPTS); 1555 1556 switch (optname) { 1557 1558 case IP_MULTICAST_IF: 1559 addr = mtod(*mp, struct in_addr *); 1560 (*mp)->m_len = sizeof(struct in_addr); 1561 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1562 *addr = zeroin_addr; 1563 else if (imo->imo_multicast_addr.s_addr) { 1564 /* return the value user has set */ 1565 *addr = imo->imo_multicast_addr; 1566 } else { 1567 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1568 *addr = ia ? ia->ia_addr.sin_addr : zeroin_addr; 1569 } 1570 return (0); 1571 1572 case IP_MULTICAST_TTL: 1573 ttl = mtod(*mp, u_char *); 1574 (*mp)->m_len = 1; 1575 *ttl = imo ? imo->imo_multicast_ttl 1576 : IP_DEFAULT_MULTICAST_TTL; 1577 return (0); 1578 1579 case IP_MULTICAST_LOOP: 1580 loop = mtod(*mp, u_char *); 1581 (*mp)->m_len = 1; 1582 *loop = imo ? imo->imo_multicast_loop 1583 : IP_DEFAULT_MULTICAST_LOOP; 1584 return (0); 1585 1586 default: 1587 return (EOPNOTSUPP); 1588 } 1589 } 1590 1591 /* 1592 * Discard the IP multicast options. 1593 */ 1594 void 1595 ip_freemoptions(imo) 1596 struct ip_moptions *imo; 1597 { 1598 int i; 1599 1600 if (imo != NULL) { 1601 for (i = 0; i < imo->imo_num_memberships; ++i) 1602 in_delmulti(imo->imo_membership[i]); 1603 free(imo, M_IPMOPTS); 1604 } 1605 } 1606 1607 /* 1608 * Routine called from ip_output() to loop back a copy of an IP multicast 1609 * packet to the input queue of a specified interface. Note that this 1610 * calls the output routine of the loopback "driver", but with an interface 1611 * pointer that might NOT be &loif -- easier than replicating that code here. 1612 */ 1613 static void 1614 ip_mloopback(ifp, m, dst) 1615 struct ifnet *ifp; 1616 struct mbuf *m; 1617 struct sockaddr_in *dst; 1618 { 1619 struct ip *ip; 1620 struct mbuf *copym; 1621 1622 copym = m_copy(m, 0, M_COPYALL); 1623 if (copym != NULL 1624 && (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip))) 1625 copym = m_pullup(copym, sizeof(struct ip)); 1626 if (copym != NULL) { 1627 /* 1628 * We don't bother to fragment if the IP length is greater 1629 * than the interface's MTU. Can this possibly matter? 1630 */ 1631 ip = mtod(copym, struct ip *); 1632 HTONS(ip->ip_len); 1633 HTONS(ip->ip_off); 1634 ip->ip_sum = 0; 1635 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2); 1636 (void) looutput(ifp, copym, sintosa(dst), NULL); 1637 } 1638 } 1639