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