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