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