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