1 /* $NetBSD: ip_output.c,v 1.39 1997/04/15 00:41:53 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1988, 1990, 1993 5 * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 36 */ 37 38 #include <sys/param.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/errno.h> 42 #include <sys/protosw.h> 43 #include <sys/socket.h> 44 #include <sys/socketvar.h> 45 #include <sys/systm.h> 46 47 #include <net/if.h> 48 #include <net/route.h> 49 #include <net/pfil.h> 50 51 #include <netinet/in.h> 52 #include <netinet/in_systm.h> 53 #include <netinet/ip.h> 54 #include <netinet/in_pcb.h> 55 #include <netinet/in_var.h> 56 #include <netinet/ip_var.h> 57 58 #ifdef vax 59 #include <machine/mtpr.h> 60 #endif 61 62 #include <machine/stdarg.h> 63 64 static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *)); 65 static void ip_mloopback 66 __P((struct ifnet *, struct mbuf *, struct sockaddr_in *)); 67 68 /* 69 * IP output. The packet in mbuf chain m contains a skeletal IP 70 * header (with len, off, ttl, proto, tos, src, dst). 71 * The mbuf chain containing the packet will be freed. 72 * The mbuf opt, if present, will not be freed. 73 */ 74 int 75 #if __STDC__ 76 ip_output(struct mbuf *m0, ...) 77 #else 78 ip_output(m0, va_alist) 79 struct mbuf *m0; 80 va_dcl 81 #endif 82 { 83 register struct ip *ip, *mhip; 84 register struct ifnet *ifp; 85 register struct mbuf *m = m0; 86 register int hlen = sizeof (struct ip); 87 int len, off, error = 0; 88 struct route iproute; 89 struct sockaddr_in *dst; 90 struct in_ifaddr *ia; 91 struct mbuf *opt; 92 struct route *ro; 93 int flags; 94 struct ip_moptions *imo; 95 va_list ap; 96 #ifdef PFIL_HOOKS 97 struct packet_filter_hook *pfh; 98 struct mbuf *m1; 99 int rv; 100 #endif /* PFIL_HOOKS */ 101 102 va_start(ap, m0); 103 opt = va_arg(ap, struct mbuf *); 104 ro = va_arg(ap, struct route *); 105 flags = va_arg(ap, int); 106 imo = va_arg(ap, struct ip_moptions *); 107 va_end(ap); 108 109 #ifdef DIAGNOSTIC 110 if ((m->m_flags & M_PKTHDR) == 0) 111 panic("ip_output no HDR"); 112 #endif 113 if (opt) { 114 m = ip_insertoptions(m, opt, &len); 115 hlen = len; 116 } 117 ip = mtod(m, struct ip *); 118 /* 119 * Fill in IP header. 120 */ 121 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 122 ip->ip_v = IPVERSION; 123 ip->ip_off &= IP_DF; 124 ip->ip_id = htons(ip_id++); 125 ip->ip_hl = hlen >> 2; 126 ipstat.ips_localout++; 127 } else { 128 hlen = ip->ip_hl << 2; 129 } 130 /* 131 * Route packet. 132 */ 133 if (ro == 0) { 134 ro = &iproute; 135 bzero((caddr_t)ro, sizeof (*ro)); 136 } 137 dst = satosin(&ro->ro_dst); 138 /* 139 * If there is a cached route, 140 * check that it is to the same destination 141 * and is still up. If not, free it and try again. 142 */ 143 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 144 !in_hosteq(dst->sin_addr, ip->ip_dst))) { 145 RTFREE(ro->ro_rt); 146 ro->ro_rt = (struct rtentry *)0; 147 } 148 if (ro->ro_rt == 0) { 149 dst->sin_family = AF_INET; 150 dst->sin_len = sizeof(*dst); 151 dst->sin_addr = ip->ip_dst; 152 } 153 /* 154 * If routing to interface only, 155 * short circuit routing lookup. 156 */ 157 if (flags & IP_ROUTETOIF) { 158 if ((ia = ifatoia(ifa_ifwithladdr(sintosa(dst)))) == 0) { 159 ipstat.ips_noroute++; 160 error = ENETUNREACH; 161 goto bad; 162 } 163 ifp = ia->ia_ifp; 164 ip->ip_ttl = 1; 165 } else { 166 if (ro->ro_rt == 0) 167 rtalloc(ro); 168 if (ro->ro_rt == 0) { 169 ipstat.ips_noroute++; 170 error = EHOSTUNREACH; 171 goto bad; 172 } 173 ia = ifatoia(ro->ro_rt->rt_ifa); 174 ifp = ro->ro_rt->rt_ifp; 175 ro->ro_rt->rt_use++; 176 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 177 dst = satosin(ro->ro_rt->rt_gateway); 178 } 179 if (IN_MULTICAST(ip->ip_dst.s_addr)) { 180 struct in_multi *inm; 181 182 m->m_flags |= M_MCAST; 183 /* 184 * IP destination address is multicast. Make sure "dst" 185 * still points to the address in "ro". (It may have been 186 * changed to point to a gateway address, above.) 187 */ 188 dst = satosin(&ro->ro_dst); 189 /* 190 * See if the caller provided any multicast options 191 */ 192 if (imo != NULL) { 193 ip->ip_ttl = imo->imo_multicast_ttl; 194 if (imo->imo_multicast_ifp != NULL) 195 ifp = imo->imo_multicast_ifp; 196 } else 197 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 198 /* 199 * Confirm that the outgoing interface supports multicast. 200 */ 201 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 202 ipstat.ips_noroute++; 203 error = ENETUNREACH; 204 goto bad; 205 } 206 /* 207 * If source address not specified yet, use address 208 * of outgoing interface. 209 */ 210 if (in_nullhost(ip->ip_src)) { 211 register struct in_ifaddr *ia; 212 213 for (ia = in_ifaddr.tqh_first; ia; ia = ia->ia_list.tqe_next) 214 if (ia->ia_ifp == ifp) { 215 ip->ip_src = ia->ia_addr.sin_addr; 216 break; 217 } 218 } 219 220 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 221 if (inm != NULL && 222 (imo == NULL || imo->imo_multicast_loop)) { 223 /* 224 * If we belong to the destination multicast group 225 * on the outgoing interface, and the caller did not 226 * forbid loopback, loop back a copy. 227 */ 228 ip_mloopback(ifp, m, dst); 229 } 230 #ifdef MROUTING 231 else { 232 /* 233 * If we are acting as a multicast router, perform 234 * multicast forwarding as if the packet had just 235 * arrived on the interface to which we are about 236 * to send. The multicast forwarding function 237 * recursively calls this function, using the 238 * IP_FORWARDING flag to prevent infinite recursion. 239 * 240 * Multicasts that are looped back by ip_mloopback(), 241 * above, will be forwarded by the ip_input() routine, 242 * if necessary. 243 */ 244 extern struct socket *ip_mrouter; 245 246 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 247 if (ip_mforward(m, ifp) != 0) { 248 m_freem(m); 249 goto done; 250 } 251 } 252 } 253 #endif 254 /* 255 * Multicasts with a time-to-live of zero may be looped- 256 * back, above, but must not be transmitted on a network. 257 * Also, multicasts addressed to the loopback interface 258 * are not sent -- the above call to ip_mloopback() will 259 * loop back a copy if this host actually belongs to the 260 * destination group on the loopback interface. 261 */ 262 if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) { 263 m_freem(m); 264 goto done; 265 } 266 267 goto sendit; 268 } 269 #ifndef notdef 270 /* 271 * If source address not specified yet, use address 272 * of outgoing interface. 273 */ 274 if (in_nullhost(ip->ip_src)) 275 ip->ip_src = ia->ia_addr.sin_addr; 276 #endif 277 /* 278 * Look for broadcast address and 279 * and verify user is allowed to send 280 * such a packet. 281 */ 282 if (in_broadcast(dst->sin_addr, ifp)) { 283 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 284 error = EADDRNOTAVAIL; 285 goto bad; 286 } 287 if ((flags & IP_ALLOWBROADCAST) == 0) { 288 error = EACCES; 289 goto bad; 290 } 291 /* don't allow broadcast messages to be fragmented */ 292 if ((u_int16_t)ip->ip_len > ifp->if_mtu) { 293 error = EMSGSIZE; 294 goto bad; 295 } 296 m->m_flags |= M_BCAST; 297 } else 298 m->m_flags &= ~M_BCAST; 299 300 #ifdef PFIL_HOOKS 301 /* 302 * Run through list of hooks for output packets. 303 */ 304 m1 = m; 305 for (pfh = pfil_hook_get(PFIL_OUT); pfh; pfh = pfh->pfil_link.le_next) 306 if (pfh->pfil_func) { 307 rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1); 308 if (rv) { 309 error = EHOSTUNREACH; 310 goto done; 311 } 312 ip = mtod(m = m1, struct ip *); 313 } 314 #endif /* PFIL_HOOKS */ 315 sendit: 316 /* 317 * If small enough for interface, can just send directly. 318 */ 319 if ((u_int16_t)ip->ip_len <= ifp->if_mtu) { 320 ip->ip_len = htons((u_int16_t)ip->ip_len); 321 ip->ip_off = htons((u_int16_t)ip->ip_off); 322 ip->ip_sum = 0; 323 ip->ip_sum = in_cksum(m, hlen); 324 error = (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt); 325 goto done; 326 } 327 /* 328 * Too large for interface; fragment if possible. 329 * Must be able to put at least 8 bytes per fragment. 330 */ 331 if (ip->ip_off & IP_DF) { 332 error = EMSGSIZE; 333 ipstat.ips_cantfrag++; 334 goto bad; 335 } 336 len = (ifp->if_mtu - hlen) &~ 7; 337 if (len < 8) { 338 error = EMSGSIZE; 339 goto bad; 340 } 341 342 { 343 int mhlen, firstlen = len; 344 struct mbuf **mnext = &m->m_nextpkt; 345 346 /* 347 * Loop through length of segment after first fragment, 348 * make new header and copy data of each part and link onto chain. 349 */ 350 m0 = m; 351 mhlen = sizeof (struct ip); 352 for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len) { 353 MGETHDR(m, M_DONTWAIT, MT_HEADER); 354 if (m == 0) { 355 error = ENOBUFS; 356 ipstat.ips_odropped++; 357 goto sendorfree; 358 } 359 *mnext = m; 360 mnext = &m->m_nextpkt; 361 m->m_data += max_linkhdr; 362 mhip = mtod(m, struct ip *); 363 *mhip = *ip; 364 if (hlen > sizeof (struct ip)) { 365 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 366 mhip->ip_hl = mhlen >> 2; 367 } 368 m->m_len = mhlen; 369 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF); 370 if (ip->ip_off & IP_MF) 371 mhip->ip_off |= IP_MF; 372 if (off + len >= (u_int16_t)ip->ip_len) 373 len = (u_int16_t)ip->ip_len - off; 374 else 375 mhip->ip_off |= IP_MF; 376 mhip->ip_len = htons((u_int16_t)(len + mhlen)); 377 m->m_next = m_copy(m0, off, len); 378 if (m->m_next == 0) { 379 error = ENOBUFS; /* ??? */ 380 ipstat.ips_odropped++; 381 goto sendorfree; 382 } 383 m->m_pkthdr.len = mhlen + len; 384 m->m_pkthdr.rcvif = (struct ifnet *)0; 385 mhip->ip_off = htons((u_int16_t)mhip->ip_off); 386 mhip->ip_sum = 0; 387 mhip->ip_sum = in_cksum(m, mhlen); 388 ipstat.ips_ofragments++; 389 } 390 /* 391 * Update first fragment by trimming what's been copied out 392 * and updating header, then send each fragment (in order). 393 */ 394 m = m0; 395 m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len); 396 m->m_pkthdr.len = hlen + firstlen; 397 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len); 398 ip->ip_off = htons((u_int16_t)(ip->ip_off | IP_MF)); 399 ip->ip_sum = 0; 400 ip->ip_sum = in_cksum(m, hlen); 401 sendorfree: 402 for (m = m0; m; m = m0) { 403 m0 = m->m_nextpkt; 404 m->m_nextpkt = 0; 405 if (error == 0) 406 error = (*ifp->if_output)(ifp, m, sintosa(dst), 407 ro->ro_rt); 408 else 409 m_freem(m); 410 } 411 412 if (error == 0) 413 ipstat.ips_fragmented++; 414 } 415 done: 416 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) { 417 RTFREE(ro->ro_rt); 418 ro->ro_rt = 0; 419 } 420 return (error); 421 bad: 422 m_freem(m); 423 goto done; 424 } 425 426 /* 427 * Insert IP options into preformed packet. 428 * Adjust IP destination as required for IP source routing, 429 * as indicated by a non-zero in_addr at the start of the options. 430 */ 431 static struct mbuf * 432 ip_insertoptions(m, opt, phlen) 433 register struct mbuf *m; 434 struct mbuf *opt; 435 int *phlen; 436 { 437 register struct ipoption *p = mtod(opt, struct ipoption *); 438 struct mbuf *n; 439 register struct ip *ip = mtod(m, struct ip *); 440 unsigned optlen; 441 442 optlen = opt->m_len - sizeof(p->ipopt_dst); 443 if (optlen + (u_int16_t)ip->ip_len > IP_MAXPACKET) 444 return (m); /* XXX should fail */ 445 if (!in_nullhost(p->ipopt_dst)) 446 ip->ip_dst = p->ipopt_dst; 447 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 448 MGETHDR(n, M_DONTWAIT, MT_HEADER); 449 if (n == 0) 450 return (m); 451 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 452 m->m_len -= sizeof(struct ip); 453 m->m_data += sizeof(struct ip); 454 n->m_next = m; 455 m = n; 456 m->m_len = optlen + sizeof(struct ip); 457 m->m_data += max_linkhdr; 458 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 459 } else { 460 m->m_data -= optlen; 461 m->m_len += optlen; 462 m->m_pkthdr.len += optlen; 463 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 464 } 465 ip = mtod(m, struct ip *); 466 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen); 467 *phlen = sizeof(struct ip) + optlen; 468 ip->ip_len += optlen; 469 return (m); 470 } 471 472 /* 473 * Copy options from ip to jp, 474 * omitting those not copied during fragmentation. 475 */ 476 int 477 ip_optcopy(ip, jp) 478 struct ip *ip, *jp; 479 { 480 register u_char *cp, *dp; 481 int opt, optlen, cnt; 482 483 cp = (u_char *)(ip + 1); 484 dp = (u_char *)(jp + 1); 485 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 486 for (; cnt > 0; cnt -= optlen, cp += optlen) { 487 opt = cp[0]; 488 if (opt == IPOPT_EOL) 489 break; 490 if (opt == IPOPT_NOP) { 491 /* Preserve for IP mcast tunnel's LSRR alignment. */ 492 *dp++ = IPOPT_NOP; 493 optlen = 1; 494 continue; 495 } else 496 optlen = cp[IPOPT_OLEN]; 497 /* bogus lengths should have been caught by ip_dooptions */ 498 if (optlen > cnt) 499 optlen = cnt; 500 if (IPOPT_COPIED(opt)) { 501 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); 502 dp += optlen; 503 } 504 } 505 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 506 *dp++ = IPOPT_EOL; 507 return (optlen); 508 } 509 510 /* 511 * IP socket option processing. 512 */ 513 int 514 ip_ctloutput(op, so, level, optname, mp) 515 int op; 516 struct socket *so; 517 int level, optname; 518 struct mbuf **mp; 519 { 520 register struct inpcb *inp = sotoinpcb(so); 521 register struct mbuf *m = *mp; 522 register int optval = 0; 523 int error = 0; 524 525 if (level != IPPROTO_IP) { 526 error = EINVAL; 527 if (op == PRCO_SETOPT && *mp) 528 (void) m_free(*mp); 529 } else switch (op) { 530 531 case PRCO_SETOPT: 532 switch (optname) { 533 case IP_OPTIONS: 534 #ifdef notyet 535 case IP_RETOPTS: 536 return (ip_pcbopts(optname, &inp->inp_options, m)); 537 #else 538 return (ip_pcbopts(&inp->inp_options, m)); 539 #endif 540 541 case IP_TOS: 542 case IP_TTL: 543 case IP_RECVOPTS: 544 case IP_RECVRETOPTS: 545 case IP_RECVDSTADDR: 546 case IP_RECVIF: 547 if (m == NULL || m->m_len != sizeof(int)) 548 error = EINVAL; 549 else { 550 optval = *mtod(m, int *); 551 switch (optname) { 552 553 case IP_TOS: 554 inp->inp_ip.ip_tos = optval; 555 break; 556 557 case IP_TTL: 558 inp->inp_ip.ip_ttl = optval; 559 break; 560 #define OPTSET(bit) \ 561 if (optval) \ 562 inp->inp_flags |= bit; \ 563 else \ 564 inp->inp_flags &= ~bit; 565 566 case IP_RECVOPTS: 567 OPTSET(INP_RECVOPTS); 568 break; 569 570 case IP_RECVRETOPTS: 571 OPTSET(INP_RECVRETOPTS); 572 break; 573 574 case IP_RECVDSTADDR: 575 OPTSET(INP_RECVDSTADDR); 576 break; 577 578 case IP_RECVIF: 579 OPTSET(INP_RECVIF); 580 break; 581 } 582 } 583 break; 584 #undef OPTSET 585 586 case IP_MULTICAST_IF: 587 case IP_MULTICAST_TTL: 588 case IP_MULTICAST_LOOP: 589 case IP_ADD_MEMBERSHIP: 590 case IP_DROP_MEMBERSHIP: 591 error = ip_setmoptions(optname, &inp->inp_moptions, m); 592 break; 593 594 default: 595 error = ENOPROTOOPT; 596 break; 597 } 598 if (m) 599 (void)m_free(m); 600 break; 601 602 case PRCO_GETOPT: 603 switch (optname) { 604 case IP_OPTIONS: 605 case IP_RETOPTS: 606 *mp = m = m_get(M_WAIT, MT_SOOPTS); 607 if (inp->inp_options) { 608 m->m_len = inp->inp_options->m_len; 609 bcopy(mtod(inp->inp_options, caddr_t), 610 mtod(m, caddr_t), (unsigned)m->m_len); 611 } else 612 m->m_len = 0; 613 break; 614 615 case IP_TOS: 616 case IP_TTL: 617 case IP_RECVOPTS: 618 case IP_RECVRETOPTS: 619 case IP_RECVDSTADDR: 620 case IP_RECVIF: 621 *mp = m = m_get(M_WAIT, MT_SOOPTS); 622 m->m_len = sizeof(int); 623 switch (optname) { 624 625 case IP_TOS: 626 optval = inp->inp_ip.ip_tos; 627 break; 628 629 case IP_TTL: 630 optval = inp->inp_ip.ip_ttl; 631 break; 632 633 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 634 635 case IP_RECVOPTS: 636 optval = OPTBIT(INP_RECVOPTS); 637 break; 638 639 case IP_RECVRETOPTS: 640 optval = OPTBIT(INP_RECVRETOPTS); 641 break; 642 643 case IP_RECVDSTADDR: 644 optval = OPTBIT(INP_RECVDSTADDR); 645 break; 646 647 case IP_RECVIF: 648 optval = OPTBIT(INP_RECVIF); 649 break; 650 } 651 *mtod(m, int *) = optval; 652 break; 653 654 case IP_MULTICAST_IF: 655 case IP_MULTICAST_TTL: 656 case IP_MULTICAST_LOOP: 657 case IP_ADD_MEMBERSHIP: 658 case IP_DROP_MEMBERSHIP: 659 error = ip_getmoptions(optname, inp->inp_moptions, mp); 660 break; 661 662 default: 663 error = ENOPROTOOPT; 664 break; 665 } 666 break; 667 } 668 return (error); 669 } 670 671 /* 672 * Set up IP options in pcb for insertion in output packets. 673 * Store in mbuf with pointer in pcbopt, adding pseudo-option 674 * with destination address if source routed. 675 */ 676 int 677 #ifdef notyet 678 ip_pcbopts(optname, pcbopt, m) 679 int optname; 680 #else 681 ip_pcbopts(pcbopt, m) 682 #endif 683 struct mbuf **pcbopt; 684 register struct mbuf *m; 685 { 686 register cnt, optlen; 687 register u_char *cp; 688 u_char opt; 689 690 /* turn off any old options */ 691 if (*pcbopt) 692 (void)m_free(*pcbopt); 693 *pcbopt = 0; 694 if (m == (struct mbuf *)0 || m->m_len == 0) { 695 /* 696 * Only turning off any previous options. 697 */ 698 if (m) 699 (void)m_free(m); 700 return (0); 701 } 702 703 #ifndef vax 704 if (m->m_len % sizeof(int32_t)) 705 goto bad; 706 #endif 707 /* 708 * IP first-hop destination address will be stored before 709 * actual options; move other options back 710 * and clear it when none present. 711 */ 712 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 713 goto bad; 714 cnt = m->m_len; 715 m->m_len += sizeof(struct in_addr); 716 cp = mtod(m, u_char *) + sizeof(struct in_addr); 717 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); 718 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 719 720 for (; cnt > 0; cnt -= optlen, cp += optlen) { 721 opt = cp[IPOPT_OPTVAL]; 722 if (opt == IPOPT_EOL) 723 break; 724 if (opt == IPOPT_NOP) 725 optlen = 1; 726 else { 727 optlen = cp[IPOPT_OLEN]; 728 if (optlen <= IPOPT_OLEN || optlen > cnt) 729 goto bad; 730 } 731 switch (opt) { 732 733 default: 734 break; 735 736 case IPOPT_LSRR: 737 case IPOPT_SSRR: 738 /* 739 * user process specifies route as: 740 * ->A->B->C->D 741 * D must be our final destination (but we can't 742 * check that since we may not have connected yet). 743 * A is first hop destination, which doesn't appear in 744 * actual IP option, but is stored before the options. 745 */ 746 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 747 goto bad; 748 m->m_len -= sizeof(struct in_addr); 749 cnt -= sizeof(struct in_addr); 750 optlen -= sizeof(struct in_addr); 751 cp[IPOPT_OLEN] = optlen; 752 /* 753 * Move first hop before start of options. 754 */ 755 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 756 sizeof(struct in_addr)); 757 /* 758 * Then copy rest of options back 759 * to close up the deleted entry. 760 */ 761 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + 762 sizeof(struct in_addr)), 763 (caddr_t)&cp[IPOPT_OFFSET+1], 764 (unsigned)cnt + sizeof(struct in_addr)); 765 break; 766 } 767 } 768 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 769 goto bad; 770 *pcbopt = m; 771 return (0); 772 773 bad: 774 (void)m_free(m); 775 return (EINVAL); 776 } 777 778 /* 779 * Set the IP multicast options in response to user setsockopt(). 780 */ 781 int 782 ip_setmoptions(optname, imop, m) 783 int optname; 784 struct ip_moptions **imop; 785 struct mbuf *m; 786 { 787 register int error = 0; 788 u_char loop; 789 register int i; 790 struct in_addr addr; 791 register struct ip_mreq *mreq; 792 register struct ifnet *ifp; 793 register struct ip_moptions *imo = *imop; 794 struct route ro; 795 register struct sockaddr_in *dst; 796 797 if (imo == NULL) { 798 /* 799 * No multicast option buffer attached to the pcb; 800 * allocate one and initialize to default values. 801 */ 802 imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS, 803 M_WAITOK); 804 805 if (imo == NULL) 806 return (ENOBUFS); 807 *imop = imo; 808 imo->imo_multicast_ifp = NULL; 809 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 810 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 811 imo->imo_num_memberships = 0; 812 } 813 814 switch (optname) { 815 816 case IP_MULTICAST_IF: 817 /* 818 * Select the interface for outgoing multicast packets. 819 */ 820 if (m == NULL || m->m_len != sizeof(struct in_addr)) { 821 error = EINVAL; 822 break; 823 } 824 addr = *(mtod(m, struct in_addr *)); 825 /* 826 * INADDR_ANY is used to remove a previous selection. 827 * When no interface is selected, a default one is 828 * chosen every time a multicast packet is sent. 829 */ 830 if (in_nullhost(addr)) { 831 imo->imo_multicast_ifp = NULL; 832 break; 833 } 834 /* 835 * The selected interface is identified by its local 836 * IP address. Find the interface and confirm that 837 * it supports multicasting. 838 */ 839 INADDR_TO_IFP(addr, ifp); 840 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 841 error = EADDRNOTAVAIL; 842 break; 843 } 844 imo->imo_multicast_ifp = ifp; 845 break; 846 847 case IP_MULTICAST_TTL: 848 /* 849 * Set the IP time-to-live for outgoing multicast packets. 850 */ 851 if (m == NULL || m->m_len != 1) { 852 error = EINVAL; 853 break; 854 } 855 imo->imo_multicast_ttl = *(mtod(m, u_char *)); 856 break; 857 858 case IP_MULTICAST_LOOP: 859 /* 860 * Set the loopback flag for outgoing multicast packets. 861 * Must be zero or one. 862 */ 863 if (m == NULL || m->m_len != 1 || 864 (loop = *(mtod(m, u_char *))) > 1) { 865 error = EINVAL; 866 break; 867 } 868 imo->imo_multicast_loop = loop; 869 break; 870 871 case IP_ADD_MEMBERSHIP: 872 /* 873 * Add a multicast group membership. 874 * Group must be a valid IP multicast address. 875 */ 876 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 877 error = EINVAL; 878 break; 879 } 880 mreq = mtod(m, struct ip_mreq *); 881 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) { 882 error = EINVAL; 883 break; 884 } 885 /* 886 * If no interface address was provided, use the interface of 887 * the route to the given multicast address. 888 */ 889 if (in_nullhost(mreq->imr_interface)) { 890 ro.ro_rt = NULL; 891 dst = satosin(&ro.ro_dst); 892 dst->sin_len = sizeof(*dst); 893 dst->sin_family = AF_INET; 894 dst->sin_addr = mreq->imr_multiaddr; 895 rtalloc(&ro); 896 if (ro.ro_rt == NULL) { 897 error = EADDRNOTAVAIL; 898 break; 899 } 900 ifp = ro.ro_rt->rt_ifp; 901 rtfree(ro.ro_rt); 902 } else { 903 INADDR_TO_IFP(mreq->imr_interface, ifp); 904 } 905 /* 906 * See if we found an interface, and confirm that it 907 * supports multicast. 908 */ 909 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 910 error = EADDRNOTAVAIL; 911 break; 912 } 913 /* 914 * See if the membership already exists or if all the 915 * membership slots are full. 916 */ 917 for (i = 0; i < imo->imo_num_memberships; ++i) { 918 if (imo->imo_membership[i]->inm_ifp == ifp && 919 in_hosteq(imo->imo_membership[i]->inm_addr, 920 mreq->imr_multiaddr)) 921 break; 922 } 923 if (i < imo->imo_num_memberships) { 924 error = EADDRINUSE; 925 break; 926 } 927 if (i == IP_MAX_MEMBERSHIPS) { 928 error = ETOOMANYREFS; 929 break; 930 } 931 /* 932 * Everything looks good; add a new record to the multicast 933 * address list for the given interface. 934 */ 935 if ((imo->imo_membership[i] = 936 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) { 937 error = ENOBUFS; 938 break; 939 } 940 ++imo->imo_num_memberships; 941 break; 942 943 case IP_DROP_MEMBERSHIP: 944 /* 945 * Drop a multicast group membership. 946 * Group must be a valid IP multicast address. 947 */ 948 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 949 error = EINVAL; 950 break; 951 } 952 mreq = mtod(m, struct ip_mreq *); 953 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) { 954 error = EINVAL; 955 break; 956 } 957 /* 958 * If an interface address was specified, get a pointer 959 * to its ifnet structure. 960 */ 961 if (in_nullhost(mreq->imr_interface)) 962 ifp = NULL; 963 else { 964 INADDR_TO_IFP(mreq->imr_interface, ifp); 965 if (ifp == NULL) { 966 error = EADDRNOTAVAIL; 967 break; 968 } 969 } 970 /* 971 * Find the membership in the membership array. 972 */ 973 for (i = 0; i < imo->imo_num_memberships; ++i) { 974 if ((ifp == NULL || 975 imo->imo_membership[i]->inm_ifp == ifp) && 976 in_hosteq(imo->imo_membership[i]->inm_addr, 977 mreq->imr_multiaddr)) 978 break; 979 } 980 if (i == imo->imo_num_memberships) { 981 error = EADDRNOTAVAIL; 982 break; 983 } 984 /* 985 * Give up the multicast address record to which the 986 * membership points. 987 */ 988 in_delmulti(imo->imo_membership[i]); 989 /* 990 * Remove the gap in the membership array. 991 */ 992 for (++i; i < imo->imo_num_memberships; ++i) 993 imo->imo_membership[i-1] = imo->imo_membership[i]; 994 --imo->imo_num_memberships; 995 break; 996 997 default: 998 error = EOPNOTSUPP; 999 break; 1000 } 1001 1002 /* 1003 * If all options have default values, no need to keep the mbuf. 1004 */ 1005 if (imo->imo_multicast_ifp == NULL && 1006 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1007 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1008 imo->imo_num_memberships == 0) { 1009 free(*imop, M_IPMOPTS); 1010 *imop = NULL; 1011 } 1012 1013 return (error); 1014 } 1015 1016 /* 1017 * Return the IP multicast options in response to user getsockopt(). 1018 */ 1019 int 1020 ip_getmoptions(optname, imo, mp) 1021 int optname; 1022 register struct ip_moptions *imo; 1023 register struct mbuf **mp; 1024 { 1025 u_char *ttl; 1026 u_char *loop; 1027 struct in_addr *addr; 1028 struct in_ifaddr *ia; 1029 1030 *mp = m_get(M_WAIT, MT_SOOPTS); 1031 1032 switch (optname) { 1033 1034 case IP_MULTICAST_IF: 1035 addr = mtod(*mp, struct in_addr *); 1036 (*mp)->m_len = sizeof(struct in_addr); 1037 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1038 *addr = zeroin_addr; 1039 else { 1040 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1041 *addr = ia ? ia->ia_addr.sin_addr : zeroin_addr; 1042 } 1043 return (0); 1044 1045 case IP_MULTICAST_TTL: 1046 ttl = mtod(*mp, u_char *); 1047 (*mp)->m_len = 1; 1048 *ttl = imo ? imo->imo_multicast_ttl 1049 : IP_DEFAULT_MULTICAST_TTL; 1050 return (0); 1051 1052 case IP_MULTICAST_LOOP: 1053 loop = mtod(*mp, u_char *); 1054 (*mp)->m_len = 1; 1055 *loop = imo ? imo->imo_multicast_loop 1056 : IP_DEFAULT_MULTICAST_LOOP; 1057 return (0); 1058 1059 default: 1060 return (EOPNOTSUPP); 1061 } 1062 } 1063 1064 /* 1065 * Discard the IP multicast options. 1066 */ 1067 void 1068 ip_freemoptions(imo) 1069 register struct ip_moptions *imo; 1070 { 1071 register int i; 1072 1073 if (imo != NULL) { 1074 for (i = 0; i < imo->imo_num_memberships; ++i) 1075 in_delmulti(imo->imo_membership[i]); 1076 free(imo, M_IPMOPTS); 1077 } 1078 } 1079 1080 /* 1081 * Routine called from ip_output() to loop back a copy of an IP multicast 1082 * packet to the input queue of a specified interface. Note that this 1083 * calls the output routine of the loopback "driver", but with an interface 1084 * pointer that might NOT be &loif -- easier than replicating that code here. 1085 */ 1086 static void 1087 ip_mloopback(ifp, m, dst) 1088 struct ifnet *ifp; 1089 register struct mbuf *m; 1090 register struct sockaddr_in *dst; 1091 { 1092 register struct ip *ip; 1093 struct mbuf *copym; 1094 1095 copym = m_copy(m, 0, M_COPYALL); 1096 if (copym != NULL) { 1097 /* 1098 * We don't bother to fragment if the IP length is greater 1099 * than the interface's MTU. Can this possibly matter? 1100 */ 1101 ip = mtod(copym, struct ip *); 1102 ip->ip_len = htons((u_int16_t)ip->ip_len); 1103 ip->ip_off = htons((u_int16_t)ip->ip_off); 1104 ip->ip_sum = 0; 1105 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2); 1106 (void) looutput(ifp, copym, sintosa(dst), NULL); 1107 } 1108 } 1109