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