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