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