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