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