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