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