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