ip_output.c revision 14611
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.31 1996/03/11 15:13:21 davidg Exp $ 35 */ 36 37#include <sys/param.h> 38#include <sys/queue.h> 39#include <sys/systm.h> 40#include <sys/malloc.h> 41#include <sys/mbuf.h> 42#include <sys/errno.h> 43#include <sys/protosw.h> 44#include <sys/socket.h> 45#include <sys/socketvar.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 * Check with the firewall... 341 */ 342 if (!(*ip_fw_chk_ptr)(m,ip,ifp,1)) { 343 error = EACCES; 344 goto done; 345 } 346 347 /* 348 * If small enough for interface, can just send directly. 349 */ 350 if ((u_short)ip->ip_len <= ifp->if_mtu) { 351 ip->ip_len = htons((u_short)ip->ip_len); 352 ip->ip_off = htons((u_short)ip->ip_off); 353 ip->ip_sum = 0; 354 ip->ip_sum = in_cksum(m, hlen); 355 error = (*ifp->if_output)(ifp, m, 356 (struct sockaddr *)dst, ro->ro_rt); 357 goto done; 358 } 359 /* 360 * Too large for interface; fragment if possible. 361 * Must be able to put at least 8 bytes per fragment. 362 */ 363 if (ip->ip_off & IP_DF) { 364 error = EMSGSIZE; 365#if 1 366 /* 367 * This case can happen if the user changed the MTU 368 * of an interface after enabling IP on it. Because 369 * most netifs don't keep track of routes pointing to 370 * them, there is no way for one to update all its 371 * routes when the MTU is changed. 372 */ 373 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) 374 && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) 375 && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 376 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 377 } 378#endif 379 ipstat.ips_cantfrag++; 380 goto bad; 381 } 382 len = (ifp->if_mtu - hlen) &~ 7; 383 if (len < 8) { 384 error = EMSGSIZE; 385 goto bad; 386 } 387 388 { 389 int mhlen, firstlen = len; 390 struct mbuf **mnext = &m->m_nextpkt; 391 392 /* 393 * Loop through length of segment after first fragment, 394 * make new header and copy data of each part and link onto chain. 395 */ 396 m0 = m; 397 mhlen = sizeof (struct ip); 398 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) { 399 MGETHDR(m, M_DONTWAIT, MT_HEADER); 400 if (m == 0) { 401 error = ENOBUFS; 402 ipstat.ips_odropped++; 403 goto sendorfree; 404 } 405 m->m_data += max_linkhdr; 406 mhip = mtod(m, struct ip *); 407 *mhip = *ip; 408 if (hlen > sizeof (struct ip)) { 409 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 410 mhip->ip_hl = mhlen >> 2; 411 } 412 m->m_len = mhlen; 413 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF); 414 if (ip->ip_off & IP_MF) 415 mhip->ip_off |= IP_MF; 416 if (off + len >= (u_short)ip->ip_len) 417 len = (u_short)ip->ip_len - off; 418 else 419 mhip->ip_off |= IP_MF; 420 mhip->ip_len = htons((u_short)(len + mhlen)); 421 m->m_next = m_copy(m0, off, len); 422 if (m->m_next == 0) { 423 (void) m_free(m); 424 error = ENOBUFS; /* ??? */ 425 ipstat.ips_odropped++; 426 goto sendorfree; 427 } 428 m->m_pkthdr.len = mhlen + len; 429 m->m_pkthdr.rcvif = (struct ifnet *)0; 430 mhip->ip_off = htons((u_short)mhip->ip_off); 431 mhip->ip_sum = 0; 432 mhip->ip_sum = in_cksum(m, mhlen); 433 *mnext = m; 434 mnext = &m->m_nextpkt; 435 ipstat.ips_ofragments++; 436 } 437 /* 438 * Update first fragment by trimming what's been copied out 439 * and updating header, then send each fragment (in order). 440 */ 441 m = m0; 442 m_adj(m, hlen + firstlen - (u_short)ip->ip_len); 443 m->m_pkthdr.len = hlen + firstlen; 444 ip->ip_len = htons((u_short)m->m_pkthdr.len); 445 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF)); 446 ip->ip_sum = 0; 447 ip->ip_sum = in_cksum(m, hlen); 448sendorfree: 449 for (m = m0; m; m = m0) { 450 m0 = m->m_nextpkt; 451 m->m_nextpkt = 0; 452 if (error == 0) 453 error = (*ifp->if_output)(ifp, m, 454 (struct sockaddr *)dst, ro->ro_rt); 455 else 456 m_freem(m); 457 } 458 459 if (error == 0) 460 ipstat.ips_fragmented++; 461 } 462done: 463 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) 464 RTFREE(ro->ro_rt); 465 466 return (error); 467bad: 468 m_freem(m0); 469 goto done; 470} 471 472/* 473 * Insert IP options into preformed packet. 474 * Adjust IP destination as required for IP source routing, 475 * as indicated by a non-zero in_addr at the start of the options. 476 * 477 * XXX This routine assumes that the packet has no options in place. 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_hl = *phlen >> 2; 517 ip->ip_len += optlen; 518 return (m); 519} 520 521/* 522 * Copy options from ip to jp, 523 * omitting those not copied during fragmentation. 524 */ 525static int 526ip_optcopy(ip, jp) 527 struct ip *ip, *jp; 528{ 529 register u_char *cp, *dp; 530 int opt, optlen, cnt; 531 532 cp = (u_char *)(ip + 1); 533 dp = (u_char *)(jp + 1); 534 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 535 for (; cnt > 0; cnt -= optlen, cp += optlen) { 536 opt = cp[0]; 537 if (opt == IPOPT_EOL) 538 break; 539 if (opt == IPOPT_NOP) { 540 /* Preserve for IP mcast tunnel's LSRR alignment. */ 541 *dp++ = IPOPT_NOP; 542 optlen = 1; 543 continue; 544 } else 545 optlen = cp[IPOPT_OLEN]; 546 /* bogus lengths should have been caught by ip_dooptions */ 547 if (optlen > cnt) 548 optlen = cnt; 549 if (IPOPT_COPIED(opt)) { 550 (void)memcpy(dp, cp, (unsigned)optlen); 551 dp += optlen; 552 } 553 } 554 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 555 *dp++ = IPOPT_EOL; 556 return (optlen); 557} 558 559/* 560 * IP socket option processing. 561 */ 562int 563ip_ctloutput(op, so, level, optname, mp) 564 int op; 565 struct socket *so; 566 int level, optname; 567 struct mbuf **mp; 568{ 569 register struct inpcb *inp = sotoinpcb(so); 570 register struct mbuf *m = *mp; 571 register int optval = 0; 572 int error = 0; 573 574 if (level != IPPROTO_IP) { 575 error = EINVAL; 576 if (op == PRCO_SETOPT && *mp) 577 (void) m_free(*mp); 578 } else switch (op) { 579 580 case PRCO_SETOPT: 581 switch (optname) { 582 case IP_OPTIONS: 583#ifdef notyet 584 case IP_RETOPTS: 585 return (ip_pcbopts(optname, &inp->inp_options, m)); 586#else 587 return (ip_pcbopts(&inp->inp_options, m)); 588#endif 589 590 case IP_TOS: 591 case IP_TTL: 592 case IP_RECVOPTS: 593 case IP_RECVRETOPTS: 594 case IP_RECVDSTADDR: 595 if (m == 0 || m->m_len != sizeof(int)) 596 error = EINVAL; 597 else { 598 optval = *mtod(m, int *); 599 switch (optname) { 600 601 case IP_TOS: 602 inp->inp_ip.ip_tos = optval; 603 break; 604 605 case IP_TTL: 606 inp->inp_ip.ip_ttl = optval; 607 break; 608#define OPTSET(bit) \ 609 if (optval) \ 610 inp->inp_flags |= bit; \ 611 else \ 612 inp->inp_flags &= ~bit; 613 614 case IP_RECVOPTS: 615 OPTSET(INP_RECVOPTS); 616 break; 617 618 case IP_RECVRETOPTS: 619 OPTSET(INP_RECVRETOPTS); 620 break; 621 622 case IP_RECVDSTADDR: 623 OPTSET(INP_RECVDSTADDR); 624 break; 625 } 626 } 627 break; 628#undef OPTSET 629 630 case IP_MULTICAST_IF: 631 case IP_MULTICAST_VIF: 632 case IP_MULTICAST_TTL: 633 case IP_MULTICAST_LOOP: 634 case IP_ADD_MEMBERSHIP: 635 case IP_DROP_MEMBERSHIP: 636 error = ip_setmoptions(optname, &inp->inp_moptions, m); 637 break; 638 639 case IP_PORTRANGE: 640 if (m == 0 || m->m_len != sizeof(int)) 641 error = EINVAL; 642 else { 643 optval = *mtod(m, int *); 644 645 switch (optval) { 646 647 case IP_PORTRANGE_DEFAULT: 648 inp->inp_flags &= ~(INP_LOWPORT); 649 inp->inp_flags &= ~(INP_HIGHPORT); 650 break; 651 652 case IP_PORTRANGE_HIGH: 653 inp->inp_flags &= ~(INP_LOWPORT); 654 inp->inp_flags |= INP_HIGHPORT; 655 break; 656 657 case IP_PORTRANGE_LOW: 658 inp->inp_flags &= ~(INP_HIGHPORT); 659 inp->inp_flags |= INP_LOWPORT; 660 break; 661 662 default: 663 error = EINVAL; 664 break; 665 } 666 } 667 668 default: 669 error = ENOPROTOOPT; 670 break; 671 } 672 if (m) 673 (void)m_free(m); 674 break; 675 676 case PRCO_GETOPT: 677 switch (optname) { 678 case IP_OPTIONS: 679 case IP_RETOPTS: 680 *mp = m = m_get(M_WAIT, MT_SOOPTS); 681 if (inp->inp_options) { 682 m->m_len = inp->inp_options->m_len; 683 (void)memcpy(mtod(m, void *), 684 mtod(inp->inp_options, void *), (unsigned)m->m_len); 685 } else 686 m->m_len = 0; 687 break; 688 689 case IP_TOS: 690 case IP_TTL: 691 case IP_RECVOPTS: 692 case IP_RECVRETOPTS: 693 case IP_RECVDSTADDR: 694 *mp = m = m_get(M_WAIT, MT_SOOPTS); 695 m->m_len = sizeof(int); 696 switch (optname) { 697 698 case IP_TOS: 699 optval = inp->inp_ip.ip_tos; 700 break; 701 702 case IP_TTL: 703 optval = inp->inp_ip.ip_ttl; 704 break; 705 706#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 707 708 case IP_RECVOPTS: 709 optval = OPTBIT(INP_RECVOPTS); 710 break; 711 712 case IP_RECVRETOPTS: 713 optval = OPTBIT(INP_RECVRETOPTS); 714 break; 715 716 case IP_RECVDSTADDR: 717 optval = OPTBIT(INP_RECVDSTADDR); 718 break; 719 } 720 *mtod(m, int *) = optval; 721 break; 722 723 case IP_MULTICAST_IF: 724 case IP_MULTICAST_VIF: 725 case IP_MULTICAST_TTL: 726 case IP_MULTICAST_LOOP: 727 case IP_ADD_MEMBERSHIP: 728 case IP_DROP_MEMBERSHIP: 729 error = ip_getmoptions(optname, inp->inp_moptions, mp); 730 break; 731 732 case IP_PORTRANGE: 733 *mp = m = m_get(M_WAIT, MT_SOOPTS); 734 m->m_len = sizeof(int); 735 736 if (inp->inp_flags & INP_HIGHPORT) 737 optval = IP_PORTRANGE_HIGH; 738 else if (inp->inp_flags & INP_LOWPORT) 739 optval = IP_PORTRANGE_LOW; 740 else 741 optval = 0; 742 743 *mtod(m, int *) = optval; 744 break; 745 746 default: 747 error = ENOPROTOOPT; 748 break; 749 } 750 break; 751 } 752 return (error); 753} 754 755/* 756 * Set up IP options in pcb for insertion in output packets. 757 * Store in mbuf with pointer in pcbopt, adding pseudo-option 758 * with destination address if source routed. 759 */ 760static int 761#ifdef notyet 762ip_pcbopts(optname, pcbopt, m) 763 int optname; 764#else 765ip_pcbopts(pcbopt, m) 766#endif 767 struct mbuf **pcbopt; 768 register struct mbuf *m; 769{ 770 register cnt, optlen; 771 register u_char *cp; 772 u_char opt; 773 774 /* turn off any old options */ 775 if (*pcbopt) 776 (void)m_free(*pcbopt); 777 *pcbopt = 0; 778 if (m == (struct mbuf *)0 || m->m_len == 0) { 779 /* 780 * Only turning off any previous options. 781 */ 782 if (m) 783 (void)m_free(m); 784 return (0); 785 } 786 787#ifndef vax 788 if (m->m_len % sizeof(long)) 789 goto bad; 790#endif 791 /* 792 * IP first-hop destination address will be stored before 793 * actual options; move other options back 794 * and clear it when none present. 795 */ 796 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 797 goto bad; 798 cnt = m->m_len; 799 m->m_len += sizeof(struct in_addr); 800 cp = mtod(m, u_char *) + sizeof(struct in_addr); 801 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); 802 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 803 804 for (; cnt > 0; cnt -= optlen, cp += optlen) { 805 opt = cp[IPOPT_OPTVAL]; 806 if (opt == IPOPT_EOL) 807 break; 808 if (opt == IPOPT_NOP) 809 optlen = 1; 810 else { 811 optlen = cp[IPOPT_OLEN]; 812 if (optlen <= IPOPT_OLEN || optlen > cnt) 813 goto bad; 814 } 815 switch (opt) { 816 817 default: 818 break; 819 820 case IPOPT_LSRR: 821 case IPOPT_SSRR: 822 /* 823 * user process specifies route as: 824 * ->A->B->C->D 825 * D must be our final destination (but we can't 826 * check that since we may not have connected yet). 827 * A is first hop destination, which doesn't appear in 828 * actual IP option, but is stored before the options. 829 */ 830 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 831 goto bad; 832 m->m_len -= sizeof(struct in_addr); 833 cnt -= sizeof(struct in_addr); 834 optlen -= sizeof(struct in_addr); 835 cp[IPOPT_OLEN] = optlen; 836 /* 837 * Move first hop before start of options. 838 */ 839 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 840 sizeof(struct in_addr)); 841 /* 842 * Then copy rest of options back 843 * to close up the deleted entry. 844 */ 845 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + 846 sizeof(struct in_addr)), 847 (caddr_t)&cp[IPOPT_OFFSET+1], 848 (unsigned)cnt + sizeof(struct in_addr)); 849 break; 850 } 851 } 852 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 853 goto bad; 854 *pcbopt = m; 855 return (0); 856 857bad: 858 (void)m_free(m); 859 return (EINVAL); 860} 861 862/* 863 * Set the IP multicast options in response to user setsockopt(). 864 */ 865static int 866ip_setmoptions(optname, imop, m) 867 int optname; 868 struct ip_moptions **imop; 869 struct mbuf *m; 870{ 871 register int error = 0; 872 u_char loop; 873 register int i; 874 struct in_addr addr; 875 register struct ip_mreq *mreq; 876 register struct ifnet *ifp; 877 register struct ip_moptions *imo = *imop; 878 struct route ro; 879 register struct sockaddr_in *dst; 880 int s; 881 882 if (imo == NULL) { 883 /* 884 * No multicast option buffer attached to the pcb; 885 * allocate one and initialize to default values. 886 */ 887 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, 888 M_WAITOK); 889 890 if (imo == NULL) 891 return (ENOBUFS); 892 *imop = imo; 893 imo->imo_multicast_ifp = NULL; 894 imo->imo_multicast_vif = -1; 895 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 896 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 897 imo->imo_num_memberships = 0; 898 } 899 900 switch (optname) { 901 /* store an index number for the vif you wanna use in the send */ 902 case IP_MULTICAST_VIF: 903 if (!legal_vif_num) { 904 error = EOPNOTSUPP; 905 break; 906 } 907 if (m == NULL || m->m_len != sizeof(int)) { 908 error = EINVAL; 909 break; 910 } 911 i = *(mtod(m, int *)); 912 if (!legal_vif_num(i) && (i != -1)) { 913 error = EINVAL; 914 break; 915 } 916 imo->imo_multicast_vif = i; 917 break; 918 919 case IP_MULTICAST_IF: 920 /* 921 * Select the interface for outgoing multicast packets. 922 */ 923 if (m == NULL || m->m_len != sizeof(struct in_addr)) { 924 error = EINVAL; 925 break; 926 } 927 addr = *(mtod(m, struct in_addr *)); 928 /* 929 * INADDR_ANY is used to remove a previous selection. 930 * When no interface is selected, a default one is 931 * chosen every time a multicast packet is sent. 932 */ 933 if (addr.s_addr == INADDR_ANY) { 934 imo->imo_multicast_ifp = NULL; 935 break; 936 } 937 /* 938 * The selected interface is identified by its local 939 * IP address. Find the interface and confirm that 940 * it supports multicasting. 941 */ 942 s = splimp(); 943 INADDR_TO_IFP(addr, ifp); 944 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 945 error = EADDRNOTAVAIL; 946 break; 947 } 948 imo->imo_multicast_ifp = ifp; 949 splx(s); 950 break; 951 952 case IP_MULTICAST_TTL: 953 /* 954 * Set the IP time-to-live for outgoing multicast packets. 955 */ 956 if (m == NULL || m->m_len != 1) { 957 error = EINVAL; 958 break; 959 } 960 imo->imo_multicast_ttl = *(mtod(m, u_char *)); 961 break; 962 963 case IP_MULTICAST_LOOP: 964 /* 965 * Set the loopback flag for outgoing multicast packets. 966 * Must be zero or one. 967 */ 968 if (m == NULL || m->m_len != 1 || 969 (loop = *(mtod(m, u_char *))) > 1) { 970 error = EINVAL; 971 break; 972 } 973 imo->imo_multicast_loop = loop; 974 break; 975 976 case IP_ADD_MEMBERSHIP: 977 /* 978 * Add a multicast group membership. 979 * Group must be a valid IP multicast address. 980 */ 981 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 982 error = EINVAL; 983 break; 984 } 985 mreq = mtod(m, struct ip_mreq *); 986 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) { 987 error = EINVAL; 988 break; 989 } 990 s = splimp(); 991 /* 992 * If no interface address was provided, use the interface of 993 * the route to the given multicast address. 994 */ 995 if (mreq->imr_interface.s_addr == INADDR_ANY) { 996 bzero((caddr_t)&ro, sizeof(ro)); 997 dst = (struct sockaddr_in *)&ro.ro_dst; 998 dst->sin_len = sizeof(*dst); 999 dst->sin_family = AF_INET; 1000 dst->sin_addr = mreq->imr_multiaddr; 1001 rtalloc(&ro); 1002 if (ro.ro_rt == NULL) { 1003 error = EADDRNOTAVAIL; 1004 splx(s); 1005 break; 1006 } 1007 ifp = ro.ro_rt->rt_ifp; 1008 rtfree(ro.ro_rt); 1009 } 1010 else { 1011 INADDR_TO_IFP(mreq->imr_interface, ifp); 1012 } 1013 1014 /* 1015 * See if we found an interface, and confirm that it 1016 * supports multicast. 1017 */ 1018 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1019 error = EADDRNOTAVAIL; 1020 splx(s); 1021 break; 1022 } 1023 /* 1024 * See if the membership already exists or if all the 1025 * membership slots are full. 1026 */ 1027 for (i = 0; i < imo->imo_num_memberships; ++i) { 1028 if (imo->imo_membership[i]->inm_ifp == ifp && 1029 imo->imo_membership[i]->inm_addr.s_addr 1030 == mreq->imr_multiaddr.s_addr) 1031 break; 1032 } 1033 if (i < imo->imo_num_memberships) { 1034 error = EADDRINUSE; 1035 splx(s); 1036 break; 1037 } 1038 if (i == IP_MAX_MEMBERSHIPS) { 1039 error = ETOOMANYREFS; 1040 splx(s); 1041 break; 1042 } 1043 /* 1044 * Everything looks good; add a new record to the multicast 1045 * address list for the given interface. 1046 */ 1047 if ((imo->imo_membership[i] = 1048 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) { 1049 error = ENOBUFS; 1050 splx(s); 1051 break; 1052 } 1053 ++imo->imo_num_memberships; 1054 splx(s); 1055 break; 1056 1057 case IP_DROP_MEMBERSHIP: 1058 /* 1059 * Drop a multicast group membership. 1060 * Group must be a valid IP multicast address. 1061 */ 1062 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 1063 error = EINVAL; 1064 break; 1065 } 1066 mreq = mtod(m, struct ip_mreq *); 1067 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) { 1068 error = EINVAL; 1069 break; 1070 } 1071 1072 s = splimp(); 1073 /* 1074 * If an interface address was specified, get a pointer 1075 * to its ifnet structure. 1076 */ 1077 if (mreq->imr_interface.s_addr == INADDR_ANY) 1078 ifp = NULL; 1079 else { 1080 INADDR_TO_IFP(mreq->imr_interface, ifp); 1081 if (ifp == NULL) { 1082 error = EADDRNOTAVAIL; 1083 splx(s); 1084 break; 1085 } 1086 } 1087 /* 1088 * Find the membership in the membership array. 1089 */ 1090 for (i = 0; i < imo->imo_num_memberships; ++i) { 1091 if ((ifp == NULL || 1092 imo->imo_membership[i]->inm_ifp == ifp) && 1093 imo->imo_membership[i]->inm_addr.s_addr == 1094 mreq->imr_multiaddr.s_addr) 1095 break; 1096 } 1097 if (i == imo->imo_num_memberships) { 1098 error = EADDRNOTAVAIL; 1099 splx(s); 1100 break; 1101 } 1102 /* 1103 * Give up the multicast address record to which the 1104 * membership points. 1105 */ 1106 in_delmulti(imo->imo_membership[i]); 1107 /* 1108 * Remove the gap in the membership array. 1109 */ 1110 for (++i; i < imo->imo_num_memberships; ++i) 1111 imo->imo_membership[i-1] = imo->imo_membership[i]; 1112 --imo->imo_num_memberships; 1113 splx(s); 1114 break; 1115 1116 default: 1117 error = EOPNOTSUPP; 1118 break; 1119 } 1120 1121 /* 1122 * If all options have default values, no need to keep the mbuf. 1123 */ 1124 if (imo->imo_multicast_ifp == NULL && 1125 imo->imo_multicast_vif == -1 && 1126 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1127 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1128 imo->imo_num_memberships == 0) { 1129 free(*imop, M_IPMOPTS); 1130 *imop = NULL; 1131 } 1132 1133 return (error); 1134} 1135 1136/* 1137 * Return the IP multicast options in response to user getsockopt(). 1138 */ 1139static int 1140ip_getmoptions(optname, imo, mp) 1141 int optname; 1142 register struct ip_moptions *imo; 1143 register struct mbuf **mp; 1144{ 1145 u_char *ttl; 1146 u_char *loop; 1147 struct in_addr *addr; 1148 struct in_ifaddr *ia; 1149 1150 *mp = m_get(M_WAIT, MT_SOOPTS); 1151 1152 switch (optname) { 1153 1154 case IP_MULTICAST_VIF: 1155 if (imo != NULL) 1156 *(mtod(*mp, int *)) = imo->imo_multicast_vif; 1157 else 1158 *(mtod(*mp, int *)) = -1; 1159 (*mp)->m_len = sizeof(int); 1160 return(0); 1161 1162 case IP_MULTICAST_IF: 1163 addr = mtod(*mp, struct in_addr *); 1164 (*mp)->m_len = sizeof(struct in_addr); 1165 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1166 addr->s_addr = INADDR_ANY; 1167 else { 1168 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1169 addr->s_addr = (ia == NULL) ? INADDR_ANY 1170 : IA_SIN(ia)->sin_addr.s_addr; 1171 } 1172 return (0); 1173 1174 case IP_MULTICAST_TTL: 1175 ttl = mtod(*mp, u_char *); 1176 (*mp)->m_len = 1; 1177 *ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL 1178 : imo->imo_multicast_ttl; 1179 return (0); 1180 1181 case IP_MULTICAST_LOOP: 1182 loop = mtod(*mp, u_char *); 1183 (*mp)->m_len = 1; 1184 *loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP 1185 : imo->imo_multicast_loop; 1186 return (0); 1187 1188 default: 1189 return (EOPNOTSUPP); 1190 } 1191} 1192 1193/* 1194 * Discard the IP multicast options. 1195 */ 1196void 1197ip_freemoptions(imo) 1198 register struct ip_moptions *imo; 1199{ 1200 register int i; 1201 1202 if (imo != NULL) { 1203 for (i = 0; i < imo->imo_num_memberships; ++i) 1204 in_delmulti(imo->imo_membership[i]); 1205 free(imo, M_IPMOPTS); 1206 } 1207} 1208 1209/* 1210 * Routine called from ip_output() to loop back a copy of an IP multicast 1211 * packet to the input queue of a specified interface. Note that this 1212 * calls the output routine of the loopback "driver", but with an interface 1213 * pointer that might NOT be a loopback interface -- evil, but easier than 1214 * replicating that code here. 1215 */ 1216static void 1217ip_mloopback(ifp, m, dst) 1218 struct ifnet *ifp; 1219 register struct mbuf *m; 1220 register struct sockaddr_in *dst; 1221{ 1222 register struct ip *ip; 1223 struct mbuf *copym; 1224 1225 copym = m_copy(m, 0, M_COPYALL); 1226 if (copym != NULL) { 1227 /* 1228 * We don't bother to fragment if the IP length is greater 1229 * than the interface's MTU. Can this possibly matter? 1230 */ 1231 ip = mtod(copym, struct ip *); 1232 ip->ip_len = htons((u_short)ip->ip_len); 1233 ip->ip_off = htons((u_short)ip->ip_off); 1234 ip->ip_sum = 0; 1235 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2); 1236 (void) looutput(ifp, copym, (struct sockaddr *)dst, NULL); 1237 } 1238} 1239