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