ip_output.c revision 169454
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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 30 * $FreeBSD: head/sys/netinet/ip_output.c 169454 2007-05-10 15:58:48Z rwatson $ 31 */ 32 33#include "opt_ipfw.h" 34#include "opt_ipsec.h" 35#include "opt_mac.h" 36#include "opt_mbuf_stress_test.h" 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/kernel.h> 41#include <sys/malloc.h> 42#include <sys/mbuf.h> 43#include <sys/priv.h> 44#include <sys/protosw.h> 45#include <sys/socket.h> 46#include <sys/socketvar.h> 47#include <sys/sysctl.h> 48 49#include <net/if.h> 50#include <net/netisr.h> 51#include <net/pfil.h> 52#include <net/route.h> 53 54#include <netinet/in.h> 55#include <netinet/in_systm.h> 56#include <netinet/ip.h> 57#include <netinet/in_pcb.h> 58#include <netinet/in_var.h> 59#include <netinet/ip_var.h> 60#include <netinet/ip_options.h> 61 62#if defined(IPSEC) || defined(FAST_IPSEC) 63#include <netinet/ip_ipsec.h> 64#ifdef IPSEC 65#include <netinet6/ipsec.h> 66#endif 67#ifdef FAST_IPSEC 68#include <netipsec/ipsec.h> 69#endif 70#endif /*IPSEC*/ 71 72#include <machine/in_cksum.h> 73 74#include <security/mac/mac_framework.h> 75 76static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options"); 77 78#define print_ip(x, a, y) printf("%s %d.%d.%d.%d%s",\ 79 x, (ntohl(a.s_addr)>>24)&0xFF,\ 80 (ntohl(a.s_addr)>>16)&0xFF,\ 81 (ntohl(a.s_addr)>>8)&0xFF,\ 82 (ntohl(a.s_addr))&0xFF, y); 83 84u_short ip_id; 85 86#ifdef MBUF_STRESS_TEST 87int mbuf_frag_size = 0; 88SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 89 &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 90#endif 91 92static struct ifnet *ip_multicast_if(struct in_addr *, int *); 93static void ip_mloopback 94 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 95static int ip_getmoptions(struct inpcb *, struct sockopt *); 96static int ip_setmoptions(struct inpcb *, struct sockopt *); 97 98 99extern struct protosw inetsw[]; 100 101/* 102 * IP output. The packet in mbuf chain m contains a skeletal IP 103 * header (with len, off, ttl, proto, tos, src, dst). 104 * The mbuf chain containing the packet will be freed. 105 * The mbuf opt, if present, will not be freed. 106 * In the IP forwarding case, the packet will arrive with options already 107 * inserted, so must have a NULL opt pointer. 108 */ 109int 110ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags, 111 struct ip_moptions *imo, struct inpcb *inp) 112{ 113 struct ip *ip; 114 struct ifnet *ifp = NULL; /* keep compiler happy */ 115 struct mbuf *m0; 116 int hlen = sizeof (struct ip); 117 int mtu; 118 int len, error = 0; 119 struct sockaddr_in *dst = NULL; /* keep compiler happy */ 120 struct in_ifaddr *ia = NULL; 121 int isbroadcast, sw_csum; 122 struct route iproute; 123 struct in_addr odst; 124#ifdef IPFIREWALL_FORWARD 125 struct m_tag *fwd_tag = NULL; 126#endif 127 M_ASSERTPKTHDR(m); 128 129 if (ro == NULL) { 130 ro = &iproute; 131 bzero(ro, sizeof (*ro)); 132 } 133 134 if (inp != NULL) 135 INP_LOCK_ASSERT(inp); 136 137 if (opt) { 138 len = 0; 139 m = ip_insertoptions(m, opt, &len); 140 if (len != 0) 141 hlen = len; 142 } 143 ip = mtod(m, struct ip *); 144 145 /* 146 * Fill in IP header. If we are not allowing fragmentation, 147 * then the ip_id field is meaningless, but we don't set it 148 * to zero. Doing so causes various problems when devices along 149 * the path (routers, load balancers, firewalls, etc.) illegally 150 * disable DF on our packet. Note that a 16-bit counter 151 * will wrap around in less than 10 seconds at 100 Mbit/s on a 152 * medium with MTU 1500. See Steven M. Bellovin, "A Technique 153 * for Counting NATted Hosts", Proc. IMW'02, available at 154 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>. 155 */ 156 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 157 ip->ip_v = IPVERSION; 158 ip->ip_hl = hlen >> 2; 159 ip->ip_id = ip_newid(); 160 ipstat.ips_localout++; 161 } else { 162 hlen = ip->ip_hl << 2; 163 } 164 165 dst = (struct sockaddr_in *)&ro->ro_dst; 166again: 167 /* 168 * If there is a cached route, 169 * check that it is to the same destination 170 * and is still up. If not, free it and try again. 171 * The address family should also be checked in case of sharing the 172 * cache with IPv6. 173 */ 174 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 175 dst->sin_family != AF_INET || 176 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 177 RTFREE(ro->ro_rt); 178 ro->ro_rt = (struct rtentry *)NULL; 179 } 180#ifdef IPFIREWALL_FORWARD 181 if (ro->ro_rt == NULL && fwd_tag == NULL) { 182#else 183 if (ro->ro_rt == NULL) { 184#endif 185 bzero(dst, sizeof(*dst)); 186 dst->sin_family = AF_INET; 187 dst->sin_len = sizeof(*dst); 188 dst->sin_addr = ip->ip_dst; 189 } 190 /* 191 * If routing to interface only, short circuit routing lookup. 192 * The use of an all-ones broadcast address implies this; an 193 * interface is specified by the broadcast address of an interface, 194 * or the destination address of a ptp interface. 195 */ 196 if (flags & IP_SENDONES) { 197 if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL && 198 (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) { 199 ipstat.ips_noroute++; 200 error = ENETUNREACH; 201 goto bad; 202 } 203 ip->ip_dst.s_addr = INADDR_BROADCAST; 204 dst->sin_addr = ip->ip_dst; 205 ifp = ia->ia_ifp; 206 ip->ip_ttl = 1; 207 isbroadcast = 1; 208 } else if (flags & IP_ROUTETOIF) { 209 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 210 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) { 211 ipstat.ips_noroute++; 212 error = ENETUNREACH; 213 goto bad; 214 } 215 ifp = ia->ia_ifp; 216 ip->ip_ttl = 1; 217 isbroadcast = in_broadcast(dst->sin_addr, ifp); 218 } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 219 imo != NULL && imo->imo_multicast_ifp != NULL) { 220 /* 221 * Bypass the normal routing lookup for multicast 222 * packets if the interface is specified. 223 */ 224 ifp = imo->imo_multicast_ifp; 225 IFP_TO_IA(ifp, ia); 226 isbroadcast = 0; /* fool gcc */ 227 } else { 228 /* 229 * We want to do any cloning requested by the link layer, 230 * as this is probably required in all cases for correct 231 * operation (as it is for ARP). 232 */ 233 if (ro->ro_rt == NULL) 234 rtalloc_ign(ro, 0); 235 if (ro->ro_rt == NULL) { 236 ipstat.ips_noroute++; 237 error = EHOSTUNREACH; 238 goto bad; 239 } 240 ia = ifatoia(ro->ro_rt->rt_ifa); 241 ifp = ro->ro_rt->rt_ifp; 242 ro->ro_rt->rt_rmx.rmx_pksent++; 243 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 244 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 245 if (ro->ro_rt->rt_flags & RTF_HOST) 246 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 247 else 248 isbroadcast = in_broadcast(dst->sin_addr, ifp); 249 } 250 /* 251 * Calculate MTU. If we have a route that is up, use that, 252 * otherwise use the interface's MTU. 253 */ 254 if (ro->ro_rt != NULL && (ro->ro_rt->rt_flags & (RTF_UP|RTF_HOST))) { 255 /* 256 * This case can happen if the user changed the MTU 257 * of an interface after enabling IP on it. Because 258 * most netifs don't keep track of routes pointing to 259 * them, there is no way for one to update all its 260 * routes when the MTU is changed. 261 */ 262 if (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu) 263 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 264 mtu = ro->ro_rt->rt_rmx.rmx_mtu; 265 } else { 266 mtu = ifp->if_mtu; 267 } 268 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 269 struct in_multi *inm; 270 271 m->m_flags |= M_MCAST; 272 /* 273 * IP destination address is multicast. Make sure "dst" 274 * still points to the address in "ro". (It may have been 275 * changed to point to a gateway address, above.) 276 */ 277 dst = (struct sockaddr_in *)&ro->ro_dst; 278 /* 279 * See if the caller provided any multicast options 280 */ 281 if (imo != NULL) { 282 ip->ip_ttl = imo->imo_multicast_ttl; 283 if (imo->imo_multicast_vif != -1) 284 ip->ip_src.s_addr = 285 ip_mcast_src ? 286 ip_mcast_src(imo->imo_multicast_vif) : 287 INADDR_ANY; 288 } else 289 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 290 /* 291 * Confirm that the outgoing interface supports multicast. 292 */ 293 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 294 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 295 ipstat.ips_noroute++; 296 error = ENETUNREACH; 297 goto bad; 298 } 299 } 300 /* 301 * If source address not specified yet, use address 302 * of outgoing interface. 303 */ 304 if (ip->ip_src.s_addr == INADDR_ANY) { 305 /* Interface may have no addresses. */ 306 if (ia != NULL) 307 ip->ip_src = IA_SIN(ia)->sin_addr; 308 } 309 310 IN_MULTI_LOCK(); 311 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 312 if (inm != NULL && 313 (imo == NULL || imo->imo_multicast_loop)) { 314 IN_MULTI_UNLOCK(); 315 /* 316 * If we belong to the destination multicast group 317 * on the outgoing interface, and the caller did not 318 * forbid loopback, loop back a copy. 319 */ 320 ip_mloopback(ifp, m, dst, hlen); 321 } 322 else { 323 IN_MULTI_UNLOCK(); 324 /* 325 * If we are acting as a multicast router, perform 326 * multicast forwarding as if the packet had just 327 * arrived on the interface to which we are about 328 * to send. The multicast forwarding function 329 * recursively calls this function, using the 330 * IP_FORWARDING flag to prevent infinite recursion. 331 * 332 * Multicasts that are looped back by ip_mloopback(), 333 * above, will be forwarded by the ip_input() routine, 334 * if necessary. 335 */ 336 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 337 /* 338 * If rsvp daemon is not running, do not 339 * set ip_moptions. This ensures that the packet 340 * is multicast and not just sent down one link 341 * as prescribed by rsvpd. 342 */ 343 if (!rsvp_on) 344 imo = NULL; 345 if (ip_mforward && 346 ip_mforward(ip, ifp, m, imo) != 0) { 347 m_freem(m); 348 goto done; 349 } 350 } 351 } 352 353 /* 354 * Multicasts with a time-to-live of zero may be looped- 355 * back, above, but must not be transmitted on a network. 356 * Also, multicasts addressed to the loopback interface 357 * are not sent -- the above call to ip_mloopback() will 358 * loop back a copy if this host actually belongs to the 359 * destination group on the loopback interface. 360 */ 361 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 362 m_freem(m); 363 goto done; 364 } 365 366 goto sendit; 367 } 368 369 /* 370 * If the source address is not specified yet, use the address 371 * of the outoing interface. 372 */ 373 if (ip->ip_src.s_addr == INADDR_ANY) { 374 /* Interface may have no addresses. */ 375 if (ia != NULL) { 376 ip->ip_src = IA_SIN(ia)->sin_addr; 377 } 378 } 379 380 /* 381 * Verify that we have any chance at all of being able to queue the 382 * packet or packet fragments, unless ALTQ is enabled on the given 383 * interface in which case packetdrop should be done by queueing. 384 */ 385#ifdef ALTQ 386 if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) && 387 ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >= 388 ifp->if_snd.ifq_maxlen)) 389#else 390 if ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >= 391 ifp->if_snd.ifq_maxlen) 392#endif /* ALTQ */ 393 { 394 error = ENOBUFS; 395 ipstat.ips_odropped++; 396 ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1); 397 goto bad; 398 } 399 400 /* 401 * Look for broadcast address and 402 * verify user is allowed to send 403 * such a packet. 404 */ 405 if (isbroadcast) { 406 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 407 error = EADDRNOTAVAIL; 408 goto bad; 409 } 410 if ((flags & IP_ALLOWBROADCAST) == 0) { 411 error = EACCES; 412 goto bad; 413 } 414 /* don't allow broadcast messages to be fragmented */ 415 if (ip->ip_len > mtu) { 416 error = EMSGSIZE; 417 goto bad; 418 } 419 m->m_flags |= M_BCAST; 420 } else { 421 m->m_flags &= ~M_BCAST; 422 } 423 424sendit: 425#if defined(IPSEC) || defined(FAST_IPSEC) 426 switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) { 427 case 1: 428 goto bad; 429 case -1: 430 goto done; 431 case 0: 432 default: 433 break; /* Continue with packet processing. */ 434 } 435 /* Update variables that are affected by ipsec4_output(). */ 436 ip = mtod(m, struct ip *); 437 hlen = ip->ip_hl << 2; 438#endif /* IPSEC */ 439 440 /* Jump over all PFIL processing if hooks are not active. */ 441 if (!PFIL_HOOKED(&inet_pfil_hook)) 442 goto passout; 443 444 /* Run through list of hooks for output packets. */ 445 odst.s_addr = ip->ip_dst.s_addr; 446 error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp); 447 if (error != 0 || m == NULL) 448 goto done; 449 450 ip = mtod(m, struct ip *); 451 452 /* See if destination IP address was changed by packet filter. */ 453 if (odst.s_addr != ip->ip_dst.s_addr) { 454 m->m_flags |= M_SKIP_FIREWALL; 455 /* If destination is now ourself drop to ip_input(). */ 456 if (in_localip(ip->ip_dst)) { 457 m->m_flags |= M_FASTFWD_OURS; 458 if (m->m_pkthdr.rcvif == NULL) 459 m->m_pkthdr.rcvif = loif; 460 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 461 m->m_pkthdr.csum_flags |= 462 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 463 m->m_pkthdr.csum_data = 0xffff; 464 } 465 m->m_pkthdr.csum_flags |= 466 CSUM_IP_CHECKED | CSUM_IP_VALID; 467 468 error = netisr_queue(NETISR_IP, m); 469 goto done; 470 } else 471 goto again; /* Redo the routing table lookup. */ 472 } 473 474#ifdef IPFIREWALL_FORWARD 475 /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 476 if (m->m_flags & M_FASTFWD_OURS) { 477 if (m->m_pkthdr.rcvif == NULL) 478 m->m_pkthdr.rcvif = loif; 479 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 480 m->m_pkthdr.csum_flags |= 481 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 482 m->m_pkthdr.csum_data = 0xffff; 483 } 484 m->m_pkthdr.csum_flags |= 485 CSUM_IP_CHECKED | CSUM_IP_VALID; 486 487 error = netisr_queue(NETISR_IP, m); 488 goto done; 489 } 490 /* Or forward to some other address? */ 491 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 492 if (fwd_tag) { 493 dst = (struct sockaddr_in *)&ro->ro_dst; 494 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 495 m->m_flags |= M_SKIP_FIREWALL; 496 m_tag_delete(m, fwd_tag); 497 goto again; 498 } 499#endif /* IPFIREWALL_FORWARD */ 500 501passout: 502 /* 127/8 must not appear on wire - RFC1122. */ 503 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 504 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 505 if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 506 ipstat.ips_badaddr++; 507 error = EADDRNOTAVAIL; 508 goto bad; 509 } 510 } 511 512 m->m_pkthdr.csum_flags |= CSUM_IP; 513 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 514 if (sw_csum & CSUM_DELAY_DATA) { 515 in_delayed_cksum(m); 516 sw_csum &= ~CSUM_DELAY_DATA; 517 } 518 m->m_pkthdr.csum_flags &= ifp->if_hwassist; 519 520 /* 521 * If small enough for interface, or the interface will take 522 * care of the fragmentation for us, we can just send directly. 523 */ 524 if (ip->ip_len <= mtu || 525 (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 || 526 ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) { 527 ip->ip_len = htons(ip->ip_len); 528 ip->ip_off = htons(ip->ip_off); 529 ip->ip_sum = 0; 530 if (sw_csum & CSUM_DELAY_IP) 531 ip->ip_sum = in_cksum(m, hlen); 532 533 /* 534 * Record statistics for this interface address. 535 * With CSUM_TSO the byte/packet count will be slightly 536 * incorrect because we count the IP+TCP headers only 537 * once instead of for every generated packet. 538 */ 539 if (!(flags & IP_FORWARDING) && ia) { 540 if (m->m_pkthdr.csum_flags & CSUM_TSO) 541 ia->ia_ifa.if_opackets += 542 m->m_pkthdr.len / m->m_pkthdr.tso_segsz; 543 else 544 ia->ia_ifa.if_opackets++; 545 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 546 } 547#ifdef IPSEC 548 /* clean ipsec history once it goes out of the node */ 549 ipsec_delaux(m); 550#endif 551#ifdef MBUF_STRESS_TEST 552 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 553 m = m_fragment(m, M_DONTWAIT, mbuf_frag_size); 554#endif 555 /* 556 * Reset layer specific mbuf flags 557 * to avoid confusing lower layers. 558 */ 559 m->m_flags &= ~(M_PROTOFLAGS); 560 561 error = (*ifp->if_output)(ifp, m, 562 (struct sockaddr *)dst, ro->ro_rt); 563 goto done; 564 } 565 566 /* Balk when DF bit is set or the interface didn't support TSO. */ 567 if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) { 568 error = EMSGSIZE; 569 ipstat.ips_cantfrag++; 570 goto bad; 571 } 572 573 /* 574 * Too large for interface; fragment if possible. If successful, 575 * on return, m will point to a list of packets to be sent. 576 */ 577 error = ip_fragment(ip, &m, mtu, ifp->if_hwassist, sw_csum); 578 if (error) 579 goto bad; 580 for (; m; m = m0) { 581 m0 = m->m_nextpkt; 582 m->m_nextpkt = 0; 583#ifdef IPSEC 584 /* clean ipsec history once it goes out of the node */ 585 ipsec_delaux(m); 586#endif 587 if (error == 0) { 588 /* Record statistics for this interface address. */ 589 if (ia != NULL) { 590 ia->ia_ifa.if_opackets++; 591 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 592 } 593 /* 594 * Reset layer specific mbuf flags 595 * to avoid confusing upper layers. 596 */ 597 m->m_flags &= ~(M_PROTOFLAGS); 598 599 error = (*ifp->if_output)(ifp, m, 600 (struct sockaddr *)dst, ro->ro_rt); 601 } else 602 m_freem(m); 603 } 604 605 if (error == 0) 606 ipstat.ips_fragmented++; 607 608done: 609 if (ro == &iproute && ro->ro_rt) { 610 RTFREE(ro->ro_rt); 611 } 612 return (error); 613bad: 614 m_freem(m); 615 goto done; 616} 617 618/* 619 * Create a chain of fragments which fit the given mtu. m_frag points to the 620 * mbuf to be fragmented; on return it points to the chain with the fragments. 621 * Return 0 if no error. If error, m_frag may contain a partially built 622 * chain of fragments that should be freed by the caller. 623 * 624 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 625 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 626 */ 627int 628ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 629 u_long if_hwassist_flags, int sw_csum) 630{ 631 int error = 0; 632 int hlen = ip->ip_hl << 2; 633 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 634 int off; 635 struct mbuf *m0 = *m_frag; /* the original packet */ 636 int firstlen; 637 struct mbuf **mnext; 638 int nfrags; 639 640 if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */ 641 ipstat.ips_cantfrag++; 642 return EMSGSIZE; 643 } 644 645 /* 646 * Must be able to put at least 8 bytes per fragment. 647 */ 648 if (len < 8) 649 return EMSGSIZE; 650 651 /* 652 * If the interface will not calculate checksums on 653 * fragmented packets, then do it here. 654 */ 655 if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 656 (if_hwassist_flags & CSUM_IP_FRAGS) == 0) { 657 in_delayed_cksum(m0); 658 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 659 } 660 661 if (len > PAGE_SIZE) { 662 /* 663 * Fragment large datagrams such that each segment 664 * contains a multiple of PAGE_SIZE amount of data, 665 * plus headers. This enables a receiver to perform 666 * page-flipping zero-copy optimizations. 667 * 668 * XXX When does this help given that sender and receiver 669 * could have different page sizes, and also mtu could 670 * be less than the receiver's page size ? 671 */ 672 int newlen; 673 struct mbuf *m; 674 675 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 676 off += m->m_len; 677 678 /* 679 * firstlen (off - hlen) must be aligned on an 680 * 8-byte boundary 681 */ 682 if (off < hlen) 683 goto smart_frag_failure; 684 off = ((off - hlen) & ~7) + hlen; 685 newlen = (~PAGE_MASK) & mtu; 686 if ((newlen + sizeof (struct ip)) > mtu) { 687 /* we failed, go back the default */ 688smart_frag_failure: 689 newlen = len; 690 off = hlen + len; 691 } 692 len = newlen; 693 694 } else { 695 off = hlen + len; 696 } 697 698 firstlen = off - hlen; 699 mnext = &m0->m_nextpkt; /* pointer to next packet */ 700 701 /* 702 * Loop through length of segment after first fragment, 703 * make new header and copy data of each part and link onto chain. 704 * Here, m0 is the original packet, m is the fragment being created. 705 * The fragments are linked off the m_nextpkt of the original 706 * packet, which after processing serves as the first fragment. 707 */ 708 for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) { 709 struct ip *mhip; /* ip header on the fragment */ 710 struct mbuf *m; 711 int mhlen = sizeof (struct ip); 712 713 MGETHDR(m, M_DONTWAIT, MT_DATA); 714 if (m == NULL) { 715 error = ENOBUFS; 716 ipstat.ips_odropped++; 717 goto done; 718 } 719 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 720 /* 721 * In the first mbuf, leave room for the link header, then 722 * copy the original IP header including options. The payload 723 * goes into an additional mbuf chain returned by m_copy(). 724 */ 725 m->m_data += max_linkhdr; 726 mhip = mtod(m, struct ip *); 727 *mhip = *ip; 728 if (hlen > sizeof (struct ip)) { 729 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 730 mhip->ip_v = IPVERSION; 731 mhip->ip_hl = mhlen >> 2; 732 } 733 m->m_len = mhlen; 734 /* XXX do we need to add ip->ip_off below ? */ 735 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 736 if (off + len >= ip->ip_len) { /* last fragment */ 737 len = ip->ip_len - off; 738 m->m_flags |= M_LASTFRAG; 739 } else 740 mhip->ip_off |= IP_MF; 741 mhip->ip_len = htons((u_short)(len + mhlen)); 742 m->m_next = m_copy(m0, off, len); 743 if (m->m_next == NULL) { /* copy failed */ 744 m_free(m); 745 error = ENOBUFS; /* ??? */ 746 ipstat.ips_odropped++; 747 goto done; 748 } 749 m->m_pkthdr.len = mhlen + len; 750 m->m_pkthdr.rcvif = NULL; 751#ifdef MAC 752 mac_create_fragment(m0, m); 753#endif 754 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 755 mhip->ip_off = htons(mhip->ip_off); 756 mhip->ip_sum = 0; 757 if (sw_csum & CSUM_DELAY_IP) 758 mhip->ip_sum = in_cksum(m, mhlen); 759 *mnext = m; 760 mnext = &m->m_nextpkt; 761 } 762 ipstat.ips_ofragments += nfrags; 763 764 /* set first marker for fragment chain */ 765 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 766 m0->m_pkthdr.csum_data = nfrags; 767 768 /* 769 * Update first fragment by trimming what's been copied out 770 * and updating header. 771 */ 772 m_adj(m0, hlen + firstlen - ip->ip_len); 773 m0->m_pkthdr.len = hlen + firstlen; 774 ip->ip_len = htons((u_short)m0->m_pkthdr.len); 775 ip->ip_off |= IP_MF; 776 ip->ip_off = htons(ip->ip_off); 777 ip->ip_sum = 0; 778 if (sw_csum & CSUM_DELAY_IP) 779 ip->ip_sum = in_cksum(m0, hlen); 780 781done: 782 *m_frag = m0; 783 return error; 784} 785 786void 787in_delayed_cksum(struct mbuf *m) 788{ 789 struct ip *ip; 790 u_short csum, offset; 791 792 ip = mtod(m, struct ip *); 793 offset = ip->ip_hl << 2 ; 794 csum = in_cksum_skip(m, ip->ip_len, offset); 795 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 796 csum = 0xffff; 797 offset += m->m_pkthdr.csum_data; /* checksum offset */ 798 799 if (offset + sizeof(u_short) > m->m_len) { 800 printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 801 m->m_len, offset, ip->ip_p); 802 /* 803 * XXX 804 * this shouldn't happen, but if it does, the 805 * correct behavior may be to insert the checksum 806 * in the appropriate next mbuf in the chain. 807 */ 808 return; 809 } 810 *(u_short *)(m->m_data + offset) = csum; 811} 812 813/* 814 * IP socket option processing. 815 */ 816int 817ip_ctloutput(struct socket *so, struct sockopt *sopt) 818{ 819 struct inpcb *inp = sotoinpcb(so); 820 int error, optval; 821 822 error = optval = 0; 823 if (sopt->sopt_level != IPPROTO_IP) { 824 return (EINVAL); 825 } 826 827 switch (sopt->sopt_dir) { 828 case SOPT_SET: 829 switch (sopt->sopt_name) { 830 case IP_OPTIONS: 831#ifdef notyet 832 case IP_RETOPTS: 833#endif 834 { 835 struct mbuf *m; 836 if (sopt->sopt_valsize > MLEN) { 837 error = EMSGSIZE; 838 break; 839 } 840 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 841 if (m == NULL) { 842 error = ENOBUFS; 843 break; 844 } 845 m->m_len = sopt->sopt_valsize; 846 error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 847 m->m_len); 848 if (error) { 849 m_free(m); 850 break; 851 } 852 INP_LOCK(inp); 853 error = ip_pcbopts(inp, sopt->sopt_name, m); 854 INP_UNLOCK(inp); 855 return (error); 856 } 857 858 case IP_TOS: 859 case IP_TTL: 860 case IP_MINTTL: 861 case IP_RECVOPTS: 862 case IP_RECVRETOPTS: 863 case IP_RECVDSTADDR: 864 case IP_RECVTTL: 865 case IP_RECVIF: 866 case IP_FAITH: 867 case IP_ONESBCAST: 868 case IP_DONTFRAG: 869 error = sooptcopyin(sopt, &optval, sizeof optval, 870 sizeof optval); 871 if (error) 872 break; 873 874 switch (sopt->sopt_name) { 875 case IP_TOS: 876 inp->inp_ip_tos = optval; 877 break; 878 879 case IP_TTL: 880 inp->inp_ip_ttl = optval; 881 break; 882 883 case IP_MINTTL: 884 if (optval > 0 && optval <= MAXTTL) 885 inp->inp_ip_minttl = optval; 886 else 887 error = EINVAL; 888 break; 889 890#define OPTSET(bit) do { \ 891 INP_LOCK(inp); \ 892 if (optval) \ 893 inp->inp_flags |= bit; \ 894 else \ 895 inp->inp_flags &= ~bit; \ 896 INP_UNLOCK(inp); \ 897} while (0) 898 899 case IP_RECVOPTS: 900 OPTSET(INP_RECVOPTS); 901 break; 902 903 case IP_RECVRETOPTS: 904 OPTSET(INP_RECVRETOPTS); 905 break; 906 907 case IP_RECVDSTADDR: 908 OPTSET(INP_RECVDSTADDR); 909 break; 910 911 case IP_RECVTTL: 912 OPTSET(INP_RECVTTL); 913 break; 914 915 case IP_RECVIF: 916 OPTSET(INP_RECVIF); 917 break; 918 919 case IP_FAITH: 920 OPTSET(INP_FAITH); 921 break; 922 923 case IP_ONESBCAST: 924 OPTSET(INP_ONESBCAST); 925 break; 926 case IP_DONTFRAG: 927 OPTSET(INP_DONTFRAG); 928 break; 929 } 930 break; 931#undef OPTSET 932 933 case IP_MULTICAST_IF: 934 case IP_MULTICAST_VIF: 935 case IP_MULTICAST_TTL: 936 case IP_MULTICAST_LOOP: 937 case IP_ADD_MEMBERSHIP: 938 case IP_DROP_MEMBERSHIP: 939 error = ip_setmoptions(inp, sopt); 940 break; 941 942 case IP_PORTRANGE: 943 error = sooptcopyin(sopt, &optval, sizeof optval, 944 sizeof optval); 945 if (error) 946 break; 947 948 INP_LOCK(inp); 949 switch (optval) { 950 case IP_PORTRANGE_DEFAULT: 951 inp->inp_flags &= ~(INP_LOWPORT); 952 inp->inp_flags &= ~(INP_HIGHPORT); 953 break; 954 955 case IP_PORTRANGE_HIGH: 956 inp->inp_flags &= ~(INP_LOWPORT); 957 inp->inp_flags |= INP_HIGHPORT; 958 break; 959 960 case IP_PORTRANGE_LOW: 961 inp->inp_flags &= ~(INP_HIGHPORT); 962 inp->inp_flags |= INP_LOWPORT; 963 break; 964 965 default: 966 error = EINVAL; 967 break; 968 } 969 INP_UNLOCK(inp); 970 break; 971 972#if defined(IPSEC) || defined(FAST_IPSEC) 973 case IP_IPSEC_POLICY: 974 { 975 caddr_t req; 976 size_t len = 0; 977 int priv; 978 struct mbuf *m; 979 int optname; 980 981 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 982 break; 983 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 984 break; 985 if (sopt->sopt_td != NULL) { 986 /* 987 * XXXRW: Would be more desirable to do this 988 * one layer down so that we only exercise 989 * privilege if it is needed. 990 */ 991 error = priv_check(sopt->sopt_td, 992 PRIV_NETINET_IPSEC); 993 if (error) 994 priv = 0; 995 else 996 priv = 1; 997 } else 998 priv = 1; 999 req = mtod(m, caddr_t); 1000 len = m->m_len; 1001 optname = sopt->sopt_name; 1002 error = ipsec4_set_policy(inp, optname, req, len, priv); 1003 m_freem(m); 1004 break; 1005 } 1006#endif /*IPSEC*/ 1007 1008 default: 1009 error = ENOPROTOOPT; 1010 break; 1011 } 1012 break; 1013 1014 case SOPT_GET: 1015 switch (sopt->sopt_name) { 1016 case IP_OPTIONS: 1017 case IP_RETOPTS: 1018 if (inp->inp_options) 1019 error = sooptcopyout(sopt, 1020 mtod(inp->inp_options, 1021 char *), 1022 inp->inp_options->m_len); 1023 else 1024 sopt->sopt_valsize = 0; 1025 break; 1026 1027 case IP_TOS: 1028 case IP_TTL: 1029 case IP_MINTTL: 1030 case IP_RECVOPTS: 1031 case IP_RECVRETOPTS: 1032 case IP_RECVDSTADDR: 1033 case IP_RECVTTL: 1034 case IP_RECVIF: 1035 case IP_PORTRANGE: 1036 case IP_FAITH: 1037 case IP_ONESBCAST: 1038 case IP_DONTFRAG: 1039 switch (sopt->sopt_name) { 1040 1041 case IP_TOS: 1042 optval = inp->inp_ip_tos; 1043 break; 1044 1045 case IP_TTL: 1046 optval = inp->inp_ip_ttl; 1047 break; 1048 1049 case IP_MINTTL: 1050 optval = inp->inp_ip_minttl; 1051 break; 1052 1053#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1054 1055 case IP_RECVOPTS: 1056 optval = OPTBIT(INP_RECVOPTS); 1057 break; 1058 1059 case IP_RECVRETOPTS: 1060 optval = OPTBIT(INP_RECVRETOPTS); 1061 break; 1062 1063 case IP_RECVDSTADDR: 1064 optval = OPTBIT(INP_RECVDSTADDR); 1065 break; 1066 1067 case IP_RECVTTL: 1068 optval = OPTBIT(INP_RECVTTL); 1069 break; 1070 1071 case IP_RECVIF: 1072 optval = OPTBIT(INP_RECVIF); 1073 break; 1074 1075 case IP_PORTRANGE: 1076 if (inp->inp_flags & INP_HIGHPORT) 1077 optval = IP_PORTRANGE_HIGH; 1078 else if (inp->inp_flags & INP_LOWPORT) 1079 optval = IP_PORTRANGE_LOW; 1080 else 1081 optval = 0; 1082 break; 1083 1084 case IP_FAITH: 1085 optval = OPTBIT(INP_FAITH); 1086 break; 1087 1088 case IP_ONESBCAST: 1089 optval = OPTBIT(INP_ONESBCAST); 1090 break; 1091 case IP_DONTFRAG: 1092 optval = OPTBIT(INP_DONTFRAG); 1093 break; 1094 } 1095 error = sooptcopyout(sopt, &optval, sizeof optval); 1096 break; 1097 1098 case IP_MULTICAST_IF: 1099 case IP_MULTICAST_VIF: 1100 case IP_MULTICAST_TTL: 1101 case IP_MULTICAST_LOOP: 1102 error = ip_getmoptions(inp, sopt); 1103 break; 1104 1105#if defined(IPSEC) || defined(FAST_IPSEC) 1106 case IP_IPSEC_POLICY: 1107 { 1108 struct mbuf *m = NULL; 1109 caddr_t req = NULL; 1110 size_t len = 0; 1111 1112 if (m != 0) { 1113 req = mtod(m, caddr_t); 1114 len = m->m_len; 1115 } 1116 error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 1117 if (error == 0) 1118 error = soopt_mcopyout(sopt, m); /* XXX */ 1119 if (error == 0) 1120 m_freem(m); 1121 break; 1122 } 1123#endif /*IPSEC*/ 1124 1125 default: 1126 error = ENOPROTOOPT; 1127 break; 1128 } 1129 break; 1130 } 1131 return (error); 1132} 1133 1134/* 1135 * XXX 1136 * The whole multicast option thing needs to be re-thought. 1137 * Several of these options are equally applicable to non-multicast 1138 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1139 * standard option (IP_TTL). 1140 */ 1141 1142/* 1143 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1144 */ 1145static struct ifnet * 1146ip_multicast_if(struct in_addr *a, int *ifindexp) 1147{ 1148 int ifindex; 1149 struct ifnet *ifp; 1150 1151 if (ifindexp) 1152 *ifindexp = 0; 1153 if (ntohl(a->s_addr) >> 24 == 0) { 1154 ifindex = ntohl(a->s_addr) & 0xffffff; 1155 if (ifindex < 0 || if_index < ifindex) 1156 return NULL; 1157 ifp = ifnet_byindex(ifindex); 1158 if (ifindexp) 1159 *ifindexp = ifindex; 1160 } else { 1161 INADDR_TO_IFP(*a, ifp); 1162 } 1163 return ifp; 1164} 1165 1166/* 1167 * Given an inpcb, return its multicast options structure pointer. Accepts 1168 * an unlocked inpcb pointer, but will return it locked. May sleep. 1169 */ 1170static struct ip_moptions * 1171ip_findmoptions(struct inpcb *inp) 1172{ 1173 struct ip_moptions *imo; 1174 struct in_multi **immp; 1175 1176 INP_LOCK(inp); 1177 if (inp->inp_moptions != NULL) 1178 return (inp->inp_moptions); 1179 1180 INP_UNLOCK(inp); 1181 1182 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK); 1183 immp = (struct in_multi **)malloc((sizeof(*immp) * IP_MIN_MEMBERSHIPS), 1184 M_IPMOPTS, M_WAITOK); 1185 1186 imo->imo_multicast_ifp = NULL; 1187 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1188 imo->imo_multicast_vif = -1; 1189 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1190 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1191 imo->imo_num_memberships = 0; 1192 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS; 1193 imo->imo_membership = immp; 1194 1195 INP_LOCK(inp); 1196 if (inp->inp_moptions != NULL) { 1197 free(immp, M_IPMOPTS); 1198 free(imo, M_IPMOPTS); 1199 return (inp->inp_moptions); 1200 } 1201 inp->inp_moptions = imo; 1202 return (imo); 1203} 1204 1205/* 1206 * Set the IP multicast options in response to user setsockopt(). 1207 */ 1208static int 1209ip_setmoptions(struct inpcb *inp, struct sockopt *sopt) 1210{ 1211 int error = 0; 1212 int i; 1213 struct in_addr addr; 1214 struct ip_mreq mreq; 1215 struct ifnet *ifp; 1216 struct ip_moptions *imo; 1217 struct route ro; 1218 struct sockaddr_in *dst; 1219 int ifindex; 1220 int s; 1221 1222 switch (sopt->sopt_name) { 1223 /* store an index number for the vif you wanna use in the send */ 1224 case IP_MULTICAST_VIF: 1225 if (legal_vif_num == 0) { 1226 error = EOPNOTSUPP; 1227 break; 1228 } 1229 error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 1230 if (error) 1231 break; 1232 if (!legal_vif_num(i) && (i != -1)) { 1233 error = EINVAL; 1234 break; 1235 } 1236 imo = ip_findmoptions(inp); 1237 imo->imo_multicast_vif = i; 1238 INP_UNLOCK(inp); 1239 break; 1240 1241 case IP_MULTICAST_IF: 1242 /* 1243 * Select the interface for outgoing multicast packets. 1244 */ 1245 error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr); 1246 if (error) 1247 break; 1248 /* 1249 * INADDR_ANY is used to remove a previous selection. 1250 * When no interface is selected, a default one is 1251 * chosen every time a multicast packet is sent. 1252 */ 1253 imo = ip_findmoptions(inp); 1254 if (addr.s_addr == INADDR_ANY) { 1255 imo->imo_multicast_ifp = NULL; 1256 INP_UNLOCK(inp); 1257 break; 1258 } 1259 /* 1260 * The selected interface is identified by its local 1261 * IP address. Find the interface and confirm that 1262 * it supports multicasting. 1263 */ 1264 s = splimp(); 1265 ifp = ip_multicast_if(&addr, &ifindex); 1266 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1267 INP_UNLOCK(inp); 1268 splx(s); 1269 error = EADDRNOTAVAIL; 1270 break; 1271 } 1272 imo->imo_multicast_ifp = ifp; 1273 if (ifindex) 1274 imo->imo_multicast_addr = addr; 1275 else 1276 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1277 INP_UNLOCK(inp); 1278 splx(s); 1279 break; 1280 1281 case IP_MULTICAST_TTL: 1282 /* 1283 * Set the IP time-to-live for outgoing multicast packets. 1284 * The original multicast API required a char argument, 1285 * which is inconsistent with the rest of the socket API. 1286 * We allow either a char or an int. 1287 */ 1288 if (sopt->sopt_valsize == 1) { 1289 u_char ttl; 1290 error = sooptcopyin(sopt, &ttl, 1, 1); 1291 if (error) 1292 break; 1293 imo = ip_findmoptions(inp); 1294 imo->imo_multicast_ttl = ttl; 1295 INP_UNLOCK(inp); 1296 } else { 1297 u_int ttl; 1298 error = sooptcopyin(sopt, &ttl, sizeof ttl, 1299 sizeof ttl); 1300 if (error) 1301 break; 1302 if (ttl > 255) 1303 error = EINVAL; 1304 else { 1305 imo = ip_findmoptions(inp); 1306 imo->imo_multicast_ttl = ttl; 1307 INP_UNLOCK(inp); 1308 } 1309 } 1310 break; 1311 1312 case IP_MULTICAST_LOOP: 1313 /* 1314 * Set the loopback flag for outgoing multicast packets. 1315 * Must be zero or one. The original multicast API required a 1316 * char argument, which is inconsistent with the rest 1317 * of the socket API. We allow either a char or an int. 1318 */ 1319 if (sopt->sopt_valsize == 1) { 1320 u_char loop; 1321 error = sooptcopyin(sopt, &loop, 1, 1); 1322 if (error) 1323 break; 1324 imo = ip_findmoptions(inp); 1325 imo->imo_multicast_loop = !!loop; 1326 INP_UNLOCK(inp); 1327 } else { 1328 u_int loop; 1329 error = sooptcopyin(sopt, &loop, sizeof loop, 1330 sizeof loop); 1331 if (error) 1332 break; 1333 imo = ip_findmoptions(inp); 1334 imo->imo_multicast_loop = !!loop; 1335 INP_UNLOCK(inp); 1336 } 1337 break; 1338 1339 case IP_ADD_MEMBERSHIP: 1340 /* 1341 * Add a multicast group membership. 1342 * Group must be a valid IP multicast address. 1343 */ 1344 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1345 if (error) 1346 break; 1347 1348 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1349 error = EINVAL; 1350 break; 1351 } 1352 s = splimp(); 1353 /* 1354 * If no interface address was provided, use the interface of 1355 * the route to the given multicast address. 1356 */ 1357 if (mreq.imr_interface.s_addr == INADDR_ANY) { 1358 bzero((caddr_t)&ro, sizeof(ro)); 1359 dst = (struct sockaddr_in *)&ro.ro_dst; 1360 dst->sin_len = sizeof(*dst); 1361 dst->sin_family = AF_INET; 1362 dst->sin_addr = mreq.imr_multiaddr; 1363 rtalloc_ign(&ro, RTF_CLONING); 1364 if (ro.ro_rt == NULL) { 1365 error = EADDRNOTAVAIL; 1366 splx(s); 1367 break; 1368 } 1369 ifp = ro.ro_rt->rt_ifp; 1370 RTFREE(ro.ro_rt); 1371 } 1372 else { 1373 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1374 } 1375 1376 /* 1377 * See if we found an interface, and confirm that it 1378 * supports multicast. 1379 */ 1380 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1381 error = EADDRNOTAVAIL; 1382 splx(s); 1383 break; 1384 } 1385 /* 1386 * See if the membership already exists or if all the 1387 * membership slots are full. 1388 */ 1389 imo = ip_findmoptions(inp); 1390 for (i = 0; i < imo->imo_num_memberships; ++i) { 1391 if (imo->imo_membership[i]->inm_ifp == ifp && 1392 imo->imo_membership[i]->inm_addr.s_addr 1393 == mreq.imr_multiaddr.s_addr) 1394 break; 1395 } 1396 if (i < imo->imo_num_memberships) { 1397 INP_UNLOCK(inp); 1398 error = EADDRINUSE; 1399 splx(s); 1400 break; 1401 } 1402 if (imo->imo_num_memberships == imo->imo_max_memberships) { 1403 struct in_multi **nmships, **omships; 1404 size_t newmax; 1405 /* 1406 * Resize the vector to next power-of-two minus 1. If the 1407 * size would exceed the maximum then we know we've really 1408 * run out of entries. Otherwise, we realloc() the vector 1409 * with the INP lock held to avoid introducing a race. 1410 */ 1411 nmships = NULL; 1412 omships = imo->imo_membership; 1413 newmax = ((imo->imo_max_memberships + 1) * 2) - 1; 1414 if (newmax <= IP_MAX_MEMBERSHIPS) { 1415 nmships = (struct in_multi **)realloc(omships, 1416sizeof(*nmships) * newmax, M_IPMOPTS, M_NOWAIT); 1417 if (nmships != NULL) { 1418 imo->imo_membership = nmships; 1419 imo->imo_max_memberships = newmax; 1420 } 1421 } 1422 if (nmships == NULL) { 1423 INP_UNLOCK(inp); 1424 error = ETOOMANYREFS; 1425 splx(s); 1426 break; 1427 } 1428 } 1429 /* 1430 * Everything looks good; add a new record to the multicast 1431 * address list for the given interface. 1432 */ 1433 if ((imo->imo_membership[i] = 1434 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 1435 INP_UNLOCK(inp); 1436 error = ENOBUFS; 1437 splx(s); 1438 break; 1439 } 1440 ++imo->imo_num_memberships; 1441 INP_UNLOCK(inp); 1442 splx(s); 1443 break; 1444 1445 case IP_DROP_MEMBERSHIP: 1446 /* 1447 * Drop a multicast group membership. 1448 * Group must be a valid IP multicast address. 1449 */ 1450 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1451 if (error) 1452 break; 1453 1454 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1455 error = EINVAL; 1456 break; 1457 } 1458 1459 s = splimp(); 1460 /* 1461 * If an interface address was specified, get a pointer 1462 * to its ifnet structure. 1463 */ 1464 if (mreq.imr_interface.s_addr == INADDR_ANY) 1465 ifp = NULL; 1466 else { 1467 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1468 if (ifp == NULL) { 1469 error = EADDRNOTAVAIL; 1470 splx(s); 1471 break; 1472 } 1473 } 1474 /* 1475 * Find the membership in the membership array. 1476 */ 1477 imo = ip_findmoptions(inp); 1478 for (i = 0; i < imo->imo_num_memberships; ++i) { 1479 if ((ifp == NULL || 1480 imo->imo_membership[i]->inm_ifp == ifp) && 1481 imo->imo_membership[i]->inm_addr.s_addr == 1482 mreq.imr_multiaddr.s_addr) 1483 break; 1484 } 1485 if (i == imo->imo_num_memberships) { 1486 INP_UNLOCK(inp); 1487 error = EADDRNOTAVAIL; 1488 splx(s); 1489 break; 1490 } 1491 /* 1492 * Give up the multicast address record to which the 1493 * membership points. 1494 */ 1495 in_delmulti(imo->imo_membership[i]); 1496 /* 1497 * Remove the gap in the membership array. 1498 */ 1499 for (++i; i < imo->imo_num_memberships; ++i) 1500 imo->imo_membership[i-1] = imo->imo_membership[i]; 1501 --imo->imo_num_memberships; 1502 INP_UNLOCK(inp); 1503 splx(s); 1504 break; 1505 1506 default: 1507 error = EOPNOTSUPP; 1508 break; 1509 } 1510 1511 return (error); 1512} 1513 1514/* 1515 * Return the IP multicast options in response to user getsockopt(). 1516 */ 1517static int 1518ip_getmoptions(struct inpcb *inp, struct sockopt *sopt) 1519{ 1520 struct ip_moptions *imo; 1521 struct in_addr addr; 1522 struct in_ifaddr *ia; 1523 int error, optval; 1524 u_char coptval; 1525 1526 INP_LOCK(inp); 1527 imo = inp->inp_moptions; 1528 1529 error = 0; 1530 switch (sopt->sopt_name) { 1531 case IP_MULTICAST_VIF: 1532 if (imo != NULL) 1533 optval = imo->imo_multicast_vif; 1534 else 1535 optval = -1; 1536 INP_UNLOCK(inp); 1537 error = sooptcopyout(sopt, &optval, sizeof optval); 1538 break; 1539 1540 case IP_MULTICAST_IF: 1541 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1542 addr.s_addr = INADDR_ANY; 1543 else if (imo->imo_multicast_addr.s_addr) { 1544 /* return the value user has set */ 1545 addr = imo->imo_multicast_addr; 1546 } else { 1547 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1548 addr.s_addr = (ia == NULL) ? INADDR_ANY 1549 : IA_SIN(ia)->sin_addr.s_addr; 1550 } 1551 INP_UNLOCK(inp); 1552 error = sooptcopyout(sopt, &addr, sizeof addr); 1553 break; 1554 1555 case IP_MULTICAST_TTL: 1556 if (imo == 0) 1557 optval = coptval = IP_DEFAULT_MULTICAST_TTL; 1558 else 1559 optval = coptval = imo->imo_multicast_ttl; 1560 INP_UNLOCK(inp); 1561 if (sopt->sopt_valsize == 1) 1562 error = sooptcopyout(sopt, &coptval, 1); 1563 else 1564 error = sooptcopyout(sopt, &optval, sizeof optval); 1565 break; 1566 1567 case IP_MULTICAST_LOOP: 1568 if (imo == 0) 1569 optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 1570 else 1571 optval = coptval = imo->imo_multicast_loop; 1572 INP_UNLOCK(inp); 1573 if (sopt->sopt_valsize == 1) 1574 error = sooptcopyout(sopt, &coptval, 1); 1575 else 1576 error = sooptcopyout(sopt, &optval, sizeof optval); 1577 break; 1578 1579 default: 1580 INP_UNLOCK(inp); 1581 error = ENOPROTOOPT; 1582 break; 1583 } 1584 INP_UNLOCK_ASSERT(inp); 1585 1586 return (error); 1587} 1588 1589/* 1590 * Discard the IP multicast options. 1591 */ 1592void 1593ip_freemoptions(struct ip_moptions *imo) 1594{ 1595 register int i; 1596 1597 if (imo != NULL) { 1598 for (i = 0; i < imo->imo_num_memberships; ++i) 1599 in_delmulti(imo->imo_membership[i]); 1600 free(imo->imo_membership, M_IPMOPTS); 1601 free(imo, M_IPMOPTS); 1602 } 1603} 1604 1605/* 1606 * Routine called from ip_output() to loop back a copy of an IP multicast 1607 * packet to the input queue of a specified interface. Note that this 1608 * calls the output routine of the loopback "driver", but with an interface 1609 * pointer that might NOT be a loopback interface -- evil, but easier than 1610 * replicating that code here. 1611 */ 1612static void 1613ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 1614 int hlen) 1615{ 1616 register struct ip *ip; 1617 struct mbuf *copym; 1618 1619 copym = m_copy(m, 0, M_COPYALL); 1620 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 1621 copym = m_pullup(copym, hlen); 1622 if (copym != NULL) { 1623 /* If needed, compute the checksum and mark it as valid. */ 1624 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1625 in_delayed_cksum(copym); 1626 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1627 copym->m_pkthdr.csum_flags |= 1628 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1629 copym->m_pkthdr.csum_data = 0xffff; 1630 } 1631 /* 1632 * We don't bother to fragment if the IP length is greater 1633 * than the interface's MTU. Can this possibly matter? 1634 */ 1635 ip = mtod(copym, struct ip *); 1636 ip->ip_len = htons(ip->ip_len); 1637 ip->ip_off = htons(ip->ip_off); 1638 ip->ip_sum = 0; 1639 ip->ip_sum = in_cksum(copym, hlen); 1640 /* 1641 * NB: 1642 * It's not clear whether there are any lingering 1643 * reentrancy problems in other areas which might 1644 * be exposed by using ip_input directly (in 1645 * particular, everything which modifies the packet 1646 * in-place). Yet another option is using the 1647 * protosw directly to deliver the looped back 1648 * packet. For the moment, we'll err on the side 1649 * of safety by using if_simloop(). 1650 */ 1651#if 1 /* XXX */ 1652 if (dst->sin_family != AF_INET) { 1653 printf("ip_mloopback: bad address family %d\n", 1654 dst->sin_family); 1655 dst->sin_family = AF_INET; 1656 } 1657#endif 1658 1659#ifdef notdef 1660 copym->m_pkthdr.rcvif = ifp; 1661 ip_input(copym); 1662#else 1663 if_simloop(ifp, copym, dst->sin_family, 0); 1664#endif 1665 } 1666} 1667