ip_output.c revision 5085
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.9 1994/11/16 10:17:10 jkh 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 * This is locally generated packet so it has not 418 * incoming interface. 419 */ 420 ip_acct_cnt(ip,NULL,ip_acct_chain,1); 421#endif 422 return (error); 423bad: 424 m_freem(m0); 425 goto done; 426} 427 428/* 429 * Insert IP options into preformed packet. 430 * Adjust IP destination as required for IP source routing, 431 * as indicated by a non-zero in_addr at the start of the options. 432 */ 433static struct mbuf * 434ip_insertoptions(m, opt, phlen) 435 register struct mbuf *m; 436 struct mbuf *opt; 437 int *phlen; 438{ 439 register struct ipoption *p = mtod(opt, struct ipoption *); 440 struct mbuf *n; 441 register struct ip *ip = mtod(m, struct ip *); 442 unsigned optlen; 443 444 optlen = opt->m_len - sizeof(p->ipopt_dst); 445 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) 446 return (m); /* XXX should fail */ 447 if (p->ipopt_dst.s_addr) 448 ip->ip_dst = p->ipopt_dst; 449 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 450 MGETHDR(n, M_DONTWAIT, MT_HEADER); 451 if (n == 0) 452 return (m); 453 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 454 m->m_len -= sizeof(struct ip); 455 m->m_data += sizeof(struct ip); 456 n->m_next = m; 457 m = n; 458 m->m_len = optlen + sizeof(struct ip); 459 m->m_data += max_linkhdr; 460 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 461 } else { 462 m->m_data -= optlen; 463 m->m_len += optlen; 464 m->m_pkthdr.len += optlen; 465 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 466 } 467 ip = mtod(m, struct ip *); 468 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen); 469 *phlen = sizeof(struct ip) + optlen; 470 ip->ip_len += optlen; 471 return (m); 472} 473 474/* 475 * Copy options from ip to jp, 476 * omitting those not copied during fragmentation. 477 */ 478int 479ip_optcopy(ip, jp) 480 struct ip *ip, *jp; 481{ 482 register u_char *cp, *dp; 483 int opt, optlen, cnt; 484 485 cp = (u_char *)(ip + 1); 486 dp = (u_char *)(jp + 1); 487 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 488 for (; cnt > 0; cnt -= optlen, cp += optlen) { 489 opt = cp[0]; 490 if (opt == IPOPT_EOL) 491 break; 492 if (opt == IPOPT_NOP) { 493 /* Preserve for IP mcast tunnel's LSRR alignment. */ 494 *dp++ = IPOPT_NOP; 495 optlen = 1; 496 continue; 497 } else 498 optlen = cp[IPOPT_OLEN]; 499 /* bogus lengths should have been caught by ip_dooptions */ 500 if (optlen > cnt) 501 optlen = cnt; 502 if (IPOPT_COPIED(opt)) { 503 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); 504 dp += optlen; 505 } 506 } 507 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 508 *dp++ = IPOPT_EOL; 509 return (optlen); 510} 511 512/* 513 * IP socket option processing. 514 */ 515int 516ip_ctloutput(op, so, level, optname, mp) 517 int op; 518 struct socket *so; 519 int level, optname; 520 struct mbuf **mp; 521{ 522 register struct inpcb *inp = sotoinpcb(so); 523 register struct mbuf *m = *mp; 524 register int optval = 0; 525 int error = 0; 526 527 if (level != IPPROTO_IP) { 528 error = EINVAL; 529 if (op == PRCO_SETOPT && *mp) 530 (void) m_free(*mp); 531 } else switch (op) { 532 533 case PRCO_SETOPT: 534 switch (optname) { 535 case IP_OPTIONS: 536#ifdef notyet 537 case IP_RETOPTS: 538 return (ip_pcbopts(optname, &inp->inp_options, m)); 539#else 540 return (ip_pcbopts(&inp->inp_options, m)); 541#endif 542 543 case IP_TOS: 544 case IP_TTL: 545 case IP_RECVOPTS: 546 case IP_RECVRETOPTS: 547 case IP_RECVDSTADDR: 548 if (m->m_len != sizeof(int)) 549 error = EINVAL; 550 else { 551 optval = *mtod(m, int *); 552 switch (optname) { 553 554 case IP_TOS: 555 inp->inp_ip.ip_tos = optval; 556 break; 557 558 case IP_TTL: 559 inp->inp_ip.ip_ttl = optval; 560 break; 561#define OPTSET(bit) \ 562 if (optval) \ 563 inp->inp_flags |= bit; \ 564 else \ 565 inp->inp_flags &= ~bit; 566 567 case IP_RECVOPTS: 568 OPTSET(INP_RECVOPTS); 569 break; 570 571 case IP_RECVRETOPTS: 572 OPTSET(INP_RECVRETOPTS); 573 break; 574 575 case IP_RECVDSTADDR: 576 OPTSET(INP_RECVDSTADDR); 577 break; 578 } 579 } 580 break; 581#undef OPTSET 582 583 case IP_MULTICAST_IF: 584 case IP_MULTICAST_VIF: 585 case IP_MULTICAST_TTL: 586 case IP_MULTICAST_LOOP: 587 case IP_ADD_MEMBERSHIP: 588 case IP_DROP_MEMBERSHIP: 589 error = ip_setmoptions(optname, &inp->inp_moptions, m); 590 break; 591 592 default: 593 error = ENOPROTOOPT; 594 break; 595 } 596 if (m) 597 (void)m_free(m); 598 break; 599 600 case PRCO_GETOPT: 601 switch (optname) { 602 case IP_OPTIONS: 603 case IP_RETOPTS: 604 *mp = m = m_get(M_WAIT, MT_SOOPTS); 605 if (inp->inp_options) { 606 m->m_len = inp->inp_options->m_len; 607 bcopy(mtod(inp->inp_options, caddr_t), 608 mtod(m, caddr_t), (unsigned)m->m_len); 609 } else 610 m->m_len = 0; 611 break; 612 613 case IP_TOS: 614 case IP_TTL: 615 case IP_RECVOPTS: 616 case IP_RECVRETOPTS: 617 case IP_RECVDSTADDR: 618 *mp = m = m_get(M_WAIT, MT_SOOPTS); 619 m->m_len = sizeof(int); 620 switch (optname) { 621 622 case IP_TOS: 623 optval = inp->inp_ip.ip_tos; 624 break; 625 626 case IP_TTL: 627 optval = inp->inp_ip.ip_ttl; 628 break; 629 630#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 631 632 case IP_RECVOPTS: 633 optval = OPTBIT(INP_RECVOPTS); 634 break; 635 636 case IP_RECVRETOPTS: 637 optval = OPTBIT(INP_RECVRETOPTS); 638 break; 639 640 case IP_RECVDSTADDR: 641 optval = OPTBIT(INP_RECVDSTADDR); 642 break; 643 } 644 *mtod(m, int *) = optval; 645 break; 646 647 case IP_MULTICAST_IF: 648 case IP_MULTICAST_VIF: 649 case IP_MULTICAST_TTL: 650 case IP_MULTICAST_LOOP: 651 case IP_ADD_MEMBERSHIP: 652 case IP_DROP_MEMBERSHIP: 653 error = ip_getmoptions(optname, inp->inp_moptions, mp); 654 break; 655 656 default: 657 error = ENOPROTOOPT; 658 break; 659 } 660 break; 661 } 662 return (error); 663} 664 665/* 666 * Set up IP options in pcb for insertion in output packets. 667 * Store in mbuf with pointer in pcbopt, adding pseudo-option 668 * with destination address if source routed. 669 */ 670int 671#ifdef notyet 672ip_pcbopts(optname, pcbopt, m) 673 int optname; 674#else 675ip_pcbopts(pcbopt, m) 676#endif 677 struct mbuf **pcbopt; 678 register struct mbuf *m; 679{ 680 register cnt, optlen; 681 register u_char *cp; 682 u_char opt; 683 684 /* turn off any old options */ 685 if (*pcbopt) 686 (void)m_free(*pcbopt); 687 *pcbopt = 0; 688 if (m == (struct mbuf *)0 || m->m_len == 0) { 689 /* 690 * Only turning off any previous options. 691 */ 692 if (m) 693 (void)m_free(m); 694 return (0); 695 } 696 697#ifndef vax 698 if (m->m_len % sizeof(long)) 699 goto bad; 700#endif 701 /* 702 * IP first-hop destination address will be stored before 703 * actual options; move other options back 704 * and clear it when none present. 705 */ 706 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 707 goto bad; 708 cnt = m->m_len; 709 m->m_len += sizeof(struct in_addr); 710 cp = mtod(m, u_char *) + sizeof(struct in_addr); 711 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); 712 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 713 714 for (; cnt > 0; cnt -= optlen, cp += optlen) { 715 opt = cp[IPOPT_OPTVAL]; 716 if (opt == IPOPT_EOL) 717 break; 718 if (opt == IPOPT_NOP) 719 optlen = 1; 720 else { 721 optlen = cp[IPOPT_OLEN]; 722 if (optlen <= IPOPT_OLEN || optlen > cnt) 723 goto bad; 724 } 725 switch (opt) { 726 727 default: 728 break; 729 730 case IPOPT_LSRR: 731 case IPOPT_SSRR: 732 /* 733 * user process specifies route as: 734 * ->A->B->C->D 735 * D must be our final destination (but we can't 736 * check that since we may not have connected yet). 737 * A is first hop destination, which doesn't appear in 738 * actual IP option, but is stored before the options. 739 */ 740 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 741 goto bad; 742 m->m_len -= sizeof(struct in_addr); 743 cnt -= sizeof(struct in_addr); 744 optlen -= sizeof(struct in_addr); 745 cp[IPOPT_OLEN] = optlen; 746 /* 747 * Move first hop before start of options. 748 */ 749 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 750 sizeof(struct in_addr)); 751 /* 752 * Then copy rest of options back 753 * to close up the deleted entry. 754 */ 755 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + 756 sizeof(struct in_addr)), 757 (caddr_t)&cp[IPOPT_OFFSET+1], 758 (unsigned)cnt + sizeof(struct in_addr)); 759 break; 760 } 761 } 762 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 763 goto bad; 764 *pcbopt = m; 765 return (0); 766 767bad: 768 (void)m_free(m); 769 return (EINVAL); 770} 771 772/* 773 * Set the IP multicast options in response to user setsockopt(). 774 */ 775int 776ip_setmoptions(optname, imop, m) 777 int optname; 778 struct ip_moptions **imop; 779 struct mbuf *m; 780{ 781 register int error = 0; 782 u_char loop; 783 register int i; 784 struct in_addr addr; 785 register struct ip_mreq *mreq; 786 register struct ifnet *ifp; 787 register struct ip_moptions *imo = *imop; 788 struct route ro; 789 register struct sockaddr_in *dst; 790 791 if (imo == NULL) { 792 /* 793 * No multicast option buffer attached to the pcb; 794 * allocate one and initialize to default values. 795 */ 796 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, 797 M_WAITOK); 798 799 if (imo == NULL) 800 return (ENOBUFS); 801 *imop = imo; 802 imo->imo_multicast_ifp = NULL; 803 imo->imo_multicast_vif = 0; 804 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 805 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 806 imo->imo_num_memberships = 0; 807 } 808 809 switch (optname) { 810 extern int (*legal_vif_num)(int); 811 /* store an index number for the vif you wanna use in the send */ 812 case IP_MULTICAST_VIF: 813 if (!legal_vif_num) { 814 error = EOPNOTSUPP; 815 break; 816 } 817 if (m == NULL || m->m_len != sizeof(int)) { 818 error = EINVAL; 819 break; 820 } 821 i = *(mtod(m, int *)); 822 if (!legal_vif_num(i)) { 823 error = EINVAL; 824 break; 825 } 826 imo->imo_multicast_vif = i; 827 break; 828 829 case IP_MULTICAST_IF: 830 /* 831 * Select the interface for outgoing multicast packets. 832 */ 833 if (m == NULL || m->m_len != sizeof(struct in_addr)) { 834 error = EINVAL; 835 break; 836 } 837 addr = *(mtod(m, struct in_addr *)); 838 /* 839 * INADDR_ANY is used to remove a previous selection. 840 * When no interface is selected, a default one is 841 * chosen every time a multicast packet is sent. 842 */ 843 if (addr.s_addr == INADDR_ANY) { 844 imo->imo_multicast_ifp = NULL; 845 break; 846 } 847 /* 848 * The selected interface is identified by its local 849 * IP address. Find the interface and confirm that 850 * it supports multicasting. 851 */ 852 INADDR_TO_IFP(addr, ifp); 853 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 854 error = EADDRNOTAVAIL; 855 break; 856 } 857 imo->imo_multicast_ifp = ifp; 858 break; 859 860 case IP_MULTICAST_TTL: 861 /* 862 * Set the IP time-to-live for outgoing multicast packets. 863 */ 864 if (m == NULL || m->m_len != 1) { 865 error = EINVAL; 866 break; 867 } 868 imo->imo_multicast_ttl = *(mtod(m, u_char *)); 869 break; 870 871 case IP_MULTICAST_LOOP: 872 /* 873 * Set the loopback flag for outgoing multicast packets. 874 * Must be zero or one. 875 */ 876 if (m == NULL || m->m_len != 1 || 877 (loop = *(mtod(m, u_char *))) > 1) { 878 error = EINVAL; 879 break; 880 } 881 imo->imo_multicast_loop = loop; 882 break; 883 884 case IP_ADD_MEMBERSHIP: 885 /* 886 * Add a multicast group membership. 887 * Group must be a valid IP multicast address. 888 */ 889 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 890 error = EINVAL; 891 break; 892 } 893 mreq = mtod(m, struct ip_mreq *); 894 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) { 895 error = EINVAL; 896 break; 897 } 898 /* 899 * If no interface address was provided, use the interface of 900 * the route to the given multicast address. 901 */ 902 if (mreq->imr_interface.s_addr == INADDR_ANY) { 903 ro.ro_rt = NULL; 904 dst = (struct sockaddr_in *)&ro.ro_dst; 905 dst->sin_len = sizeof(*dst); 906 dst->sin_family = AF_INET; 907 dst->sin_addr = mreq->imr_multiaddr; 908 rtalloc(&ro); 909 if (ro.ro_rt == NULL) { 910 error = EADDRNOTAVAIL; 911 break; 912 } 913 ifp = ro.ro_rt->rt_ifp; 914 rtfree(ro.ro_rt); 915 } 916 else { 917 INADDR_TO_IFP(mreq->imr_interface, ifp); 918 } 919 /* 920 * See if we found an interface, and confirm that it 921 * supports multicast. 922 */ 923 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 924 error = EADDRNOTAVAIL; 925 break; 926 } 927 /* 928 * See if the membership already exists or if all the 929 * membership slots are full. 930 */ 931 for (i = 0; i < imo->imo_num_memberships; ++i) { 932 if (imo->imo_membership[i]->inm_ifp == ifp && 933 imo->imo_membership[i]->inm_addr.s_addr 934 == mreq->imr_multiaddr.s_addr) 935 break; 936 } 937 if (i < imo->imo_num_memberships) { 938 error = EADDRINUSE; 939 break; 940 } 941 if (i == IP_MAX_MEMBERSHIPS) { 942 error = ETOOMANYREFS; 943 break; 944 } 945 /* 946 * Everything looks good; add a new record to the multicast 947 * address list for the given interface. 948 */ 949 if ((imo->imo_membership[i] = 950 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) { 951 error = ENOBUFS; 952 break; 953 } 954 ++imo->imo_num_memberships; 955 break; 956 957 case IP_DROP_MEMBERSHIP: 958 /* 959 * Drop a multicast group membership. 960 * Group must be a valid IP multicast address. 961 */ 962 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 963 error = EINVAL; 964 break; 965 } 966 mreq = mtod(m, struct ip_mreq *); 967 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) { 968 error = EINVAL; 969 break; 970 } 971 /* 972 * If an interface address was specified, get a pointer 973 * to its ifnet structure. 974 */ 975 if (mreq->imr_interface.s_addr == INADDR_ANY) 976 ifp = NULL; 977 else { 978 INADDR_TO_IFP(mreq->imr_interface, ifp); 979 if (ifp == NULL) { 980 error = EADDRNOTAVAIL; 981 break; 982 } 983 } 984 /* 985 * Find the membership in the membership array. 986 */ 987 for (i = 0; i < imo->imo_num_memberships; ++i) { 988 if ((ifp == NULL || 989 imo->imo_membership[i]->inm_ifp == ifp) && 990 imo->imo_membership[i]->inm_addr.s_addr == 991 mreq->imr_multiaddr.s_addr) 992 break; 993 } 994 if (i == imo->imo_num_memberships) { 995 error = EADDRNOTAVAIL; 996 break; 997 } 998 /* 999 * Give up the multicast address record to which the 1000 * membership points. 1001 */ 1002 in_delmulti(imo->imo_membership[i]); 1003 /* 1004 * Remove the gap in the membership array. 1005 */ 1006 for (++i; i < imo->imo_num_memberships; ++i) 1007 imo->imo_membership[i-1] = imo->imo_membership[i]; 1008 --imo->imo_num_memberships; 1009 break; 1010 1011 default: 1012 error = EOPNOTSUPP; 1013 break; 1014 } 1015 1016 /* 1017 * If all options have default values, no need to keep the mbuf. 1018 */ 1019 if (imo->imo_multicast_ifp == NULL && 1020 imo->imo_multicast_vif == 0 && 1021 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1022 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1023 imo->imo_num_memberships == 0) { 1024 free(*imop, M_IPMOPTS); 1025 *imop = NULL; 1026 } 1027 1028 return (error); 1029} 1030 1031/* 1032 * Return the IP multicast options in response to user getsockopt(). 1033 */ 1034int 1035ip_getmoptions(optname, imo, mp) 1036 int optname; 1037 register struct ip_moptions *imo; 1038 register struct mbuf **mp; 1039{ 1040 u_char *ttl; 1041 u_char *loop; 1042 struct in_addr *addr; 1043 struct in_ifaddr *ia; 1044 1045 *mp = m_get(M_WAIT, MT_SOOPTS); 1046 1047 switch (optname) { 1048 1049 case IP_MULTICAST_VIF: 1050 if (imo != NULL) 1051 *(mtod(*mp, int *)) = imo->imo_multicast_vif; 1052 else 1053 *(mtod(*mp, int *)) = 7890; 1054 (*mp)->m_len = sizeof(int); 1055 return(0); 1056 1057 case IP_MULTICAST_IF: 1058 addr = mtod(*mp, struct in_addr *); 1059 (*mp)->m_len = sizeof(struct in_addr); 1060 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1061 addr->s_addr = INADDR_ANY; 1062 else { 1063 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1064 addr->s_addr = (ia == NULL) ? INADDR_ANY 1065 : IA_SIN(ia)->sin_addr.s_addr; 1066 } 1067 return (0); 1068 1069 case IP_MULTICAST_TTL: 1070 ttl = mtod(*mp, u_char *); 1071 (*mp)->m_len = 1; 1072 *ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL 1073 : imo->imo_multicast_ttl; 1074 return (0); 1075 1076 case IP_MULTICAST_LOOP: 1077 loop = mtod(*mp, u_char *); 1078 (*mp)->m_len = 1; 1079 *loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP 1080 : imo->imo_multicast_loop; 1081 return (0); 1082 1083 default: 1084 return (EOPNOTSUPP); 1085 } 1086} 1087 1088/* 1089 * Discard the IP multicast options. 1090 */ 1091void 1092ip_freemoptions(imo) 1093 register struct ip_moptions *imo; 1094{ 1095 register int i; 1096 1097 if (imo != NULL) { 1098 for (i = 0; i < imo->imo_num_memberships; ++i) 1099 in_delmulti(imo->imo_membership[i]); 1100 free(imo, M_IPMOPTS); 1101 } 1102} 1103 1104/* 1105 * Routine called from ip_output() to loop back a copy of an IP multicast 1106 * packet to the input queue of a specified interface. Note that this 1107 * calls the output routine of the loopback "driver", but with an interface 1108 * pointer that might NOT be &loif -- easier than replicating that code here. 1109 */ 1110static void 1111ip_mloopback(ifp, m, dst) 1112 struct ifnet *ifp; 1113 register struct mbuf *m; 1114 register struct sockaddr_in *dst; 1115{ 1116 register struct ip *ip; 1117 struct mbuf *copym; 1118 1119 copym = m_copy(m, 0, M_COPYALL); 1120 if (copym != NULL) { 1121 /* 1122 * We don't bother to fragment if the IP length is greater 1123 * than the interface's MTU. Can this possibly matter? 1124 */ 1125 ip = mtod(copym, struct ip *); 1126 ip->ip_len = htons((u_short)ip->ip_len); 1127 ip->ip_off = htons((u_short)ip->ip_off); 1128 ip->ip_sum = 0; 1129 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2); 1130 (void) looutput(ifp, copym, (struct sockaddr *)dst, NULL); 1131 } 1132} 1133