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