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 * $FreeBSD$ 35 */ 36 37 #define _IP_VHL 38 39 #include <sys/param.h> 40 #include <sys/queue.h> 41 #include <sys/systm.h> 42 #include <sys/malloc.h> 43 #include <sys/mbuf.h> 44 #include <sys/errno.h> 45 #include <sys/protosw.h> 46 #include <sys/socket.h> 47 #include <sys/socketvar.h> 48 49 #include <net/if.h> 50 #include <net/route.h> 51 52 #include <netinet/in.h> 53 #include <netinet/in_systm.h> 54 #include <netinet/ip.h> 55 #include <netinet/in_pcb.h> 56 #include <netinet/in_var.h> 57 #include <netinet/ip_var.h> 58 59 #ifdef vax 60 #include <machine/mtpr.h> 61 #endif 62 #include <machine/in_cksum.h> 63 64 #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1 65 #undef COMPAT_IPFW 66 #define COMPAT_IPFW 1 67 #else 68 #undef COMPAT_IPFW 69 #endif 70 71 u_short ip_id; 72 73 static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *)); 74 static void ip_mloopback 75 __P((struct ifnet *, struct mbuf *, struct sockaddr_in *)); 76 static int ip_getmoptions 77 __P((int, struct ip_moptions *, struct mbuf **)); 78 static int ip_optcopy __P((struct ip *, struct ip *)); 79 static int ip_pcbopts __P((struct mbuf **, struct mbuf *)); 80 static int ip_setmoptions 81 __P((int, struct ip_moptions **, struct mbuf *)); 82 83 #if defined(IPFILTER_LKM) || defined(IPFILTER) 84 extern int fr_check __P((struct ip *, int, struct ifnet *, int, struct mbuf **)); 85 extern int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)); 86 #endif 87 88 89 extern struct protosw inetsw[]; 90 91 /* 92 * IP output. The packet in mbuf chain m contains a skeletal IP 93 * header (with len, off, ttl, proto, tos, src, dst). 94 * The mbuf chain containing the packet will be freed. 95 * The mbuf opt, if present, will not be freed. 96 */ 97 int 98 ip_output(m0, opt, ro, flags, imo) 99 struct mbuf *m0; 100 struct mbuf *opt; 101 struct route *ro; 102 int flags; 103 struct ip_moptions *imo; 104 { 105 struct ip *ip, *mhip; 106 struct ifnet *ifp; 107 struct mbuf *m = m0; 108 int hlen = sizeof (struct ip); 109 int len, off, error = 0; 110 struct sockaddr_in *dst; 111 struct in_ifaddr *ia; 112 int isbroadcast; 113 114 #ifdef DIAGNOSTIC 115 if ((m->m_flags & M_PKTHDR) == 0) 116 panic("ip_output no HDR"); 117 if (!ro) 118 panic("ip_output no route, proto = %d", 119 mtod(m, struct ip *)->ip_p); 120 #endif 121 if (opt) { 122 m = ip_insertoptions(m, opt, &len); 123 hlen = len; 124 } 125 ip = mtod(m, struct ip *); 126 /* 127 * Fill in IP header. 128 */ 129 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 130 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2); 131 ip->ip_off &= IP_DF; 132 ip->ip_id = htons(ip_id++); 133 ipstat.ips_localout++; 134 } else { 135 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 136 } 137 138 dst = (struct sockaddr_in *)&ro->ro_dst; 139 /* 140 * If there is a cached route, 141 * check that it is to the same destination 142 * and is still up. If not, free it and try again. 143 */ 144 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 145 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 146 RTFREE(ro->ro_rt); 147 ro->ro_rt = (struct rtentry *)0; 148 } 149 if (ro->ro_rt == 0) { 150 dst->sin_family = AF_INET; 151 dst->sin_len = sizeof(*dst); 152 dst->sin_addr = ip->ip_dst; 153 } 154 /* 155 * If routing to interface only, 156 * short circuit routing lookup. 157 */ 158 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa)) 159 #define sintosa(sin) ((struct sockaddr *)(sin)) 160 if (flags & IP_ROUTETOIF) { 161 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 && 162 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) { 163 ipstat.ips_noroute++; 164 error = ENETUNREACH; 165 goto bad; 166 } 167 ifp = ia->ia_ifp; 168 ip->ip_ttl = 1; 169 isbroadcast = in_broadcast(dst->sin_addr, ifp); 170 } else { 171 /* 172 * If this is the case, we probably don't want to allocate 173 * a protocol-cloned route since we didn't get one from the 174 * ULP. This lets TCP do its thing, while not burdening 175 * forwarding or ICMP with the overhead of cloning a route. 176 * Of course, we still want to do any cloning requested by 177 * the link layer, as this is probably required in all cases 178 * for correct operation (as it is for ARP). 179 */ 180 if (ro->ro_rt == 0) 181 rtalloc_ign(ro, RTF_PRCLONING); 182 if (ro->ro_rt == 0) { 183 ipstat.ips_noroute++; 184 error = EHOSTUNREACH; 185 goto bad; 186 } 187 ia = ifatoia(ro->ro_rt->rt_ifa); 188 ifp = ro->ro_rt->rt_ifp; 189 ro->ro_rt->rt_use++; 190 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 191 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 192 if (ro->ro_rt->rt_flags & RTF_HOST) 193 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 194 else 195 isbroadcast = in_broadcast(dst->sin_addr, ifp); 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_ifaddrhead.tqh_first; ia; 237 ia = ia->ia_link.tqe_next) 238 if (ia->ia_ifp == ifp) { 239 ip->ip_src = IA_SIN(ia)->sin_addr; 240 break; 241 } 242 } 243 244 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 245 if (inm != NULL && 246 (imo == NULL || imo->imo_multicast_loop)) { 247 /* 248 * If we belong to the destination multicast group 249 * on the outgoing interface, and the caller did not 250 * forbid loopback, loop back a copy. 251 */ 252 ip_mloopback(ifp, m, dst); 253 } 254 else { 255 /* 256 * If we are acting as a multicast router, perform 257 * multicast forwarding as if the packet had just 258 * arrived on the interface to which we are about 259 * to send. The multicast forwarding function 260 * recursively calls this function, using the 261 * IP_FORWARDING flag to prevent infinite recursion. 262 * 263 * Multicasts that are looped back by ip_mloopback(), 264 * above, will be forwarded by the ip_input() routine, 265 * if necessary. 266 */ 267 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 268 /* 269 * Check if rsvp daemon is running. If not, don't 270 * set ip_moptions. This ensures that the packet 271 * is multicast and not just sent down one link 272 * as prescribed by rsvpd. 273 */ 274 if (!rsvp_on) 275 imo = NULL; 276 if (ip_mforward(ip, ifp, m, imo) != 0) { 277 m_freem(m); 278 goto done; 279 } 280 } 281 } 282 283 /* 284 * Multicasts with a time-to-live of zero may be looped- 285 * back, above, but must not be transmitted on a network. 286 * Also, multicasts addressed to the loopback interface 287 * are not sent -- the above call to ip_mloopback() will 288 * loop back a copy if this host actually belongs to the 289 * destination group on the loopback interface. 290 */ 291 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 292 m_freem(m); 293 goto done; 294 } 295 296 goto sendit; 297 } 298 #ifndef notdef 299 /* 300 * If source address not specified yet, use address 301 * of outgoing interface. 302 */ 303 if (ip->ip_src.s_addr == INADDR_ANY) 304 ip->ip_src = IA_SIN(ia)->sin_addr; 305 #endif 306 /* 307 * Verify that we have any chance at all of being able to queue 308 * the packet or packet fragments 309 */ 310 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 311 ifp->if_snd.ifq_maxlen) { 312 error = ENOBUFS; 313 goto bad; 314 } 315 316 /* 317 * Look for broadcast address and 318 * and verify user is allowed to send 319 * such a packet. 320 */ 321 if (isbroadcast) { 322 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 323 error = EADDRNOTAVAIL; 324 goto bad; 325 } 326 if ((flags & IP_ALLOWBROADCAST) == 0) { 327 error = EACCES; 328 goto bad; 329 } 330 /* don't allow broadcast messages to be fragmented */ 331 if ((u_short)ip->ip_len > ifp->if_mtu) { 332 error = EMSGSIZE; 333 goto bad; 334 } 335 m->m_flags |= M_BCAST; 336 } else { 337 m->m_flags &= ~M_BCAST; 338 } 339 340 #if defined(IPFILTER) || defined(IPFILTER_LKM) 341 { 342 struct mbuf *m0 = m; 343 /* 344 * looks like most checking has been done now...do a filter check 345 */ 346 if (fr_checkp && (*fr_checkp)(ip, hlen, ifp, 1, &m0)) 347 { 348 error = EHOSTUNREACH; 349 goto done; 350 } 351 ip = mtod(m = m0, struct ip *); 352 } 353 #endif 354 sendit: 355 /* 356 * IpHack's section. 357 * - Xlate: translate packet's addr/port (NAT). 358 * - Firewall: deny/allow 359 * - Wrap: fake packet's addr/port <unimpl.> 360 * - Encapsulate: put it in another IP and send out. <unimp.> 361 */ 362 363 #ifdef COMPAT_IPFW 364 if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, ifp, IP_NAT_OUT)) { 365 error = EACCES; 366 goto done; 367 } 368 369 /* 370 * Check with the firewall... 371 */ 372 if (ip_fw_chk_ptr) { 373 int action; 374 375 #ifdef IPDIVERT 376 action = (*ip_fw_chk_ptr)(&ip, 377 hlen, ifp, (~0 << 16) | ip_divert_ignore, &m); 378 ip_divert_ignore = 0; 379 #else 380 action = (*ip_fw_chk_ptr)(&ip, hlen, ifp, (~0 << 16), &m); 381 #endif 382 if (action == -1) { 383 error = EACCES; /* XXX is this appropriate? */ 384 goto done; 385 } else if (action != 0) { 386 #ifdef IPDIVERT 387 ip_divert_port = action; /* divert to port */ 388 (*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, 0); 389 goto done; 390 #else 391 m_freem(m); /* ipfw says divert, but we can't */ 392 goto done; 393 #endif 394 } 395 } 396 #endif /* COMPAT_IPFW */ 397 398 /* 399 * If small enough for interface, can just send directly. 400 */ 401 if ((u_short)ip->ip_len <= ifp->if_mtu) { 402 ip->ip_len = htons((u_short)ip->ip_len); 403 ip->ip_off = htons((u_short)ip->ip_off); 404 ip->ip_sum = 0; 405 if (ip->ip_vhl == IP_VHL_BORING) { 406 ip->ip_sum = in_cksum_hdr(ip); 407 } else { 408 ip->ip_sum = in_cksum(m, hlen); 409 } 410 error = (*ifp->if_output)(ifp, m, 411 (struct sockaddr *)dst, ro->ro_rt); 412 goto done; 413 } 414 /* 415 * Too large for interface; fragment if possible. 416 * Must be able to put at least 8 bytes per fragment. 417 */ 418 if (ip->ip_off & IP_DF) { 419 error = EMSGSIZE; 420 /* 421 * This case can happen if the user changed the MTU 422 * of an interface after enabling IP on it. Because 423 * most netifs don't keep track of routes pointing to 424 * them, there is no way for one to update all its 425 * routes when the MTU is changed. 426 */ 427 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) 428 && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) 429 && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 430 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 431 } 432 ipstat.ips_cantfrag++; 433 goto bad; 434 } 435 len = (ifp->if_mtu - hlen) &~ 7; 436 if (len < 8) { 437 error = EMSGSIZE; 438 goto bad; 439 } 440 441 { 442 int mhlen, firstlen = len; 443 struct mbuf **mnext = &m->m_nextpkt; 444 445 /* 446 * Loop through length of segment after first fragment, 447 * make new header and copy data of each part and link onto chain. 448 */ 449 m0 = m; 450 mhlen = sizeof (struct ip); 451 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) { 452 MGETHDR(m, M_DONTWAIT, MT_HEADER); 453 if (m == 0) { 454 error = ENOBUFS; 455 ipstat.ips_odropped++; 456 goto sendorfree; 457 } 458 m->m_data += max_linkhdr; 459 mhip = mtod(m, struct ip *); 460 *mhip = *ip; 461 if (hlen > sizeof (struct ip)) { 462 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 463 mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2); 464 } 465 m->m_len = mhlen; 466 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF); 467 if (ip->ip_off & IP_MF) 468 mhip->ip_off |= IP_MF; 469 if (off + len >= (u_short)ip->ip_len) 470 len = (u_short)ip->ip_len - off; 471 else 472 mhip->ip_off |= IP_MF; 473 mhip->ip_len = htons((u_short)(len + mhlen)); 474 m->m_next = m_copy(m0, off, len); 475 if (m->m_next == 0) { 476 (void) m_free(m); 477 error = ENOBUFS; /* ??? */ 478 ipstat.ips_odropped++; 479 goto sendorfree; 480 } 481 m->m_pkthdr.len = mhlen + len; 482 m->m_pkthdr.rcvif = (struct ifnet *)0; 483 mhip->ip_off = htons((u_short)mhip->ip_off); 484 mhip->ip_sum = 0; 485 if (mhip->ip_vhl == IP_VHL_BORING) { 486 mhip->ip_sum = in_cksum_hdr(mhip); 487 } else { 488 mhip->ip_sum = in_cksum(m, mhlen); 489 } 490 *mnext = m; 491 mnext = &m->m_nextpkt; 492 ipstat.ips_ofragments++; 493 } 494 /* 495 * Update first fragment by trimming what's been copied out 496 * and updating header, then send each fragment (in order). 497 */ 498 m = m0; 499 m_adj(m, hlen + firstlen - (u_short)ip->ip_len); 500 m->m_pkthdr.len = hlen + firstlen; 501 ip->ip_len = htons((u_short)m->m_pkthdr.len); 502 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF)); 503 ip->ip_sum = 0; 504 if (ip->ip_vhl == IP_VHL_BORING) { 505 ip->ip_sum = in_cksum_hdr(ip); 506 } else { 507 ip->ip_sum = in_cksum(m, hlen); 508 } 509 sendorfree: 510 for (m = m0; m; m = m0) { 511 m0 = m->m_nextpkt; 512 m->m_nextpkt = 0; 513 if (error == 0) 514 error = (*ifp->if_output)(ifp, m, 515 (struct sockaddr *)dst, ro->ro_rt); 516 else 517 m_freem(m); 518 } 519 520 if (error == 0) 521 ipstat.ips_fragmented++; 522 } 523 done: 524 return (error); 525 bad: 526 m_freem(m0); 527 goto done; 528 } 529 530 /* 531 * Insert IP options into preformed packet. 532 * Adjust IP destination as required for IP source routing, 533 * as indicated by a non-zero in_addr at the start of the options. 534 * 535 * XXX This routine assumes that the packet has no options in place. 536 */ 537 static struct mbuf * 538 ip_insertoptions(m, opt, phlen) 539 register struct mbuf *m; 540 struct mbuf *opt; 541 int *phlen; 542 { 543 register struct ipoption *p = mtod(opt, struct ipoption *); 544 struct mbuf *n; 545 register struct ip *ip = mtod(m, struct ip *); 546 unsigned optlen; 547 548 optlen = opt->m_len - sizeof(p->ipopt_dst); 549 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) 550 return (m); /* XXX should fail */ 551 if (p->ipopt_dst.s_addr) 552 ip->ip_dst = p->ipopt_dst; 553 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 554 MGETHDR(n, M_DONTWAIT, MT_HEADER); 555 if (n == 0) 556 return (m); 557 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 558 m->m_len -= sizeof(struct ip); 559 m->m_data += sizeof(struct ip); 560 n->m_next = m; 561 m = n; 562 m->m_len = optlen + sizeof(struct ip); 563 m->m_data += max_linkhdr; 564 (void)memcpy(mtod(m, void *), ip, sizeof(struct ip)); 565 } else { 566 m->m_data -= optlen; 567 m->m_len += optlen; 568 m->m_pkthdr.len += optlen; 569 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 570 } 571 ip = mtod(m, struct ip *); 572 bcopy(p->ipopt_list, ip + 1, optlen); 573 *phlen = sizeof(struct ip) + optlen; 574 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2); 575 ip->ip_len += optlen; 576 return (m); 577 } 578 579 /* 580 * Copy options from ip to jp, 581 * omitting those not copied during fragmentation. 582 */ 583 static int 584 ip_optcopy(ip, jp) 585 struct ip *ip, *jp; 586 { 587 register u_char *cp, *dp; 588 int opt, optlen, cnt; 589 590 cp = (u_char *)(ip + 1); 591 dp = (u_char *)(jp + 1); 592 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 593 for (; cnt > 0; cnt -= optlen, cp += optlen) { 594 opt = cp[0]; 595 if (opt == IPOPT_EOL) 596 break; 597 if (opt == IPOPT_NOP) { 598 /* Preserve for IP mcast tunnel's LSRR alignment. */ 599 *dp++ = IPOPT_NOP; 600 optlen = 1; 601 continue; 602 } else 603 optlen = cp[IPOPT_OLEN]; 604 /* bogus lengths should have been caught by ip_dooptions */ 605 if (optlen > cnt) 606 optlen = cnt; 607 if (IPOPT_COPIED(opt)) { 608 bcopy(cp, dp, optlen); 609 dp += optlen; 610 } 611 } 612 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 613 *dp++ = IPOPT_EOL; 614 return (optlen); 615 } 616 617 /* 618 * IP socket option processing. 619 */ 620 int 621 ip_ctloutput(op, so, level, optname, mp) 622 int op; 623 struct socket *so; 624 int level, optname; 625 struct mbuf **mp; 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.ip_tos = optval; 662 break; 663 664 case IP_TTL: 665 inp->inp_ip.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.ip_tos; 765 break; 766 767 case IP_TTL: 768 optval = inp->inp_ip.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 */ 829 static int 830 #ifdef notyet 831 ip_pcbopts(optname, pcbopt, m) 832 int optname; 833 #else 834 ip_pcbopts(pcbopt, m) 835 #endif 836 struct mbuf **pcbopt; 837 register struct mbuf *m; 838 { 839 register 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 926 bad: 927 (void)m_free(m); 928 return (EINVAL); 929 } 930 931 /* 932 * Set the IP multicast options in response to user setsockopt(). 933 */ 934 static int 935 ip_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 */ 1209 static int 1210 ip_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 */ 1266 void 1267 ip_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 */ 1286 static void 1287 ip_mloopback(ifp, m, dst) 1288 struct ifnet *ifp; 1289 register struct mbuf *m; 1290 register struct sockaddr_in *dst; 1291 { 1292 register struct ip *ip; 1293 struct mbuf *copym; 1294 1295 copym = m_copy(m, 0, M_COPYALL); 1296 if (copym != NULL) { 1297 /* 1298 * We don't bother to fragment if the IP length is greater 1299 * than the interface's MTU. Can this possibly matter? 1300 */ 1301 ip = mtod(copym, struct ip *); 1302 ip->ip_len = htons((u_short)ip->ip_len); 1303 ip->ip_off = htons((u_short)ip->ip_off); 1304 ip->ip_sum = 0; 1305 if (ip->ip_vhl == IP_VHL_BORING) { 1306 ip->ip_sum = in_cksum_hdr(ip); 1307 } else { 1308 ip->ip_sum = in_cksum(copym, 1309 IP_VHL_HL(ip->ip_vhl) << 2); 1310 } 1311 /* 1312 * NB: 1313 * We can't simply call ip_input() directly because 1314 * the ip_mforward() depends on the `input interface' 1315 * being set to something unreasonable so that we don't 1316 * attempt to forward the looped-back copy. 1317 * It's also not clear whether there are any lingering 1318 * reentrancy problems in other areas which might be 1319 * exposed by this code. For the moment, we'll err 1320 * on the side of safety by continuing to abuse 1321 * loinput(). 1322 */ 1323 #ifdef notdef 1324 copym->m_pkthdr.rcvif = &loif[0]; 1325 ip_input(copym) 1326 #else 1327 (void) looutput(ifp, copym, (struct sockaddr *)dst, NULL); 1328 #endif 1329 } 1330 } 1331