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. 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: src/sys/netinet/ip_output.c,v 1.99.2.37 2003/04/15 06:44:45 silby Exp $ 31 * $DragonFly: src/sys/netinet/ip_output.c,v 1.67 2008/10/28 03:07:28 sephe Exp $ 32 */ 33 34 #define _IP_VHL 35 36 #include "opt_ipfw.h" 37 #include "opt_ipdn.h" 38 #include "opt_ipdivert.h" 39 #include "opt_ipfilter.h" 40 #include "opt_ipsec.h" 41 #include "opt_mbuf_stress_test.h" 42 #include "opt_mpls.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 #include <sys/proc.h> 53 #include <sys/priv.h> 54 #include <sys/sysctl.h> 55 #include <sys/in_cksum.h> 56 #include <sys/lock.h> 57 58 #include <sys/thread2.h> 59 #include <sys/mplock2.h> 60 61 #include <net/if.h> 62 #include <net/netisr.h> 63 #include <net/pfil.h> 64 #include <net/route.h> 65 66 #include <netinet/in.h> 67 #include <netinet/in_systm.h> 68 #include <netinet/ip.h> 69 #include <netinet/in_pcb.h> 70 #include <netinet/in_var.h> 71 #include <netinet/ip_var.h> 72 73 #include <netproto/mpls/mpls_var.h> 74 75 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options"); 76 77 #ifdef IPSEC 78 #include <netinet6/ipsec.h> 79 #include <netproto/key/key.h> 80 #ifdef IPSEC_DEBUG 81 #include <netproto/key/key_debug.h> 82 #else 83 #define KEYDEBUG(lev,arg) 84 #endif 85 #endif /*IPSEC*/ 86 87 #ifdef FAST_IPSEC 88 #include <netproto/ipsec/ipsec.h> 89 #include <netproto/ipsec/xform.h> 90 #include <netproto/ipsec/key.h> 91 #endif /*FAST_IPSEC*/ 92 93 #include <net/ipfw/ip_fw.h> 94 #include <net/dummynet/ip_dummynet.h> 95 96 #define print_ip(x, a, y) kprintf("%s %d.%d.%d.%d%s",\ 97 x, (ntohl(a.s_addr)>>24)&0xFF,\ 98 (ntohl(a.s_addr)>>16)&0xFF,\ 99 (ntohl(a.s_addr)>>8)&0xFF,\ 100 (ntohl(a.s_addr))&0xFF, y); 101 102 u_short ip_id; 103 104 #ifdef MBUF_STRESS_TEST 105 int mbuf_frag_size = 0; 106 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 107 &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 108 #endif 109 110 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *); 111 static struct ifnet *ip_multicast_if(struct in_addr *, int *); 112 static void ip_mloopback 113 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 114 static int ip_getmoptions 115 (struct sockopt *, struct ip_moptions *); 116 static int ip_pcbopts(int, struct mbuf **, struct mbuf *); 117 static int ip_setmoptions 118 (struct sockopt *, struct ip_moptions **); 119 120 int ip_optcopy(struct ip *, struct ip *); 121 122 extern int route_assert_owner_access; 123 124 extern struct protosw inetsw[]; 125 126 static int 127 ip_localforward(struct mbuf *m, const struct sockaddr_in *dst, int hlen) 128 { 129 struct in_ifaddr_container *iac; 130 131 /* 132 * We need to figure out if we have been forwarded to a local 133 * socket. If so, then we should somehow "loop back" to 134 * ip_input(), and get directed to the PCB as if we had received 135 * this packet. This is because it may be difficult to identify 136 * the packets you want to forward until they are being output 137 * and have selected an interface (e.g. locally initiated 138 * packets). If we used the loopback inteface, we would not be 139 * able to control what happens as the packet runs through 140 * ip_input() as it is done through a ISR. 141 */ 142 LIST_FOREACH(iac, INADDR_HASH(dst->sin_addr.s_addr), ia_hash) { 143 /* 144 * If the addr to forward to is one of ours, we pretend 145 * to be the destination for this packet. 146 */ 147 if (IA_SIN(iac->ia)->sin_addr.s_addr == dst->sin_addr.s_addr) 148 break; 149 } 150 if (iac != NULL) { 151 struct ip *ip; 152 153 if (m->m_pkthdr.rcvif == NULL) 154 m->m_pkthdr.rcvif = ifunit("lo0"); 155 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 156 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | 157 CSUM_PSEUDO_HDR; 158 m->m_pkthdr.csum_data = 0xffff; 159 } 160 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED | CSUM_IP_VALID; 161 162 /* 163 * Make sure that the IP header is in one mbuf, 164 * required by ip_input 165 */ 166 if (m->m_len < hlen) { 167 m = m_pullup(m, hlen); 168 if (m == NULL) { 169 /* The packet was freed; we are done */ 170 return 1; 171 } 172 } 173 ip = mtod(m, struct ip *); 174 175 ip->ip_len = htons(ip->ip_len); 176 ip->ip_off = htons(ip->ip_off); 177 ip_input(m); 178 179 return 1; /* The packet gets forwarded locally */ 180 } 181 return 0; 182 } 183 184 /* 185 * IP output. The packet in mbuf chain m contains a skeletal IP 186 * header (with len, off, ttl, proto, tos, src, dst). 187 * The mbuf chain containing the packet will be freed. 188 * The mbuf opt, if present, will not be freed. 189 */ 190 int 191 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, 192 int flags, struct ip_moptions *imo, struct inpcb *inp) 193 { 194 struct ip *ip; 195 struct ifnet *ifp = NULL; /* keep compiler happy */ 196 struct mbuf *m; 197 int hlen = sizeof(struct ip); 198 int len, error = 0; 199 struct sockaddr_in *dst = NULL; /* keep compiler happy */ 200 struct in_ifaddr *ia = NULL; 201 int isbroadcast, sw_csum; 202 struct in_addr pkt_dst; 203 struct route iproute; 204 struct m_tag *mtag; 205 #ifdef IPSEC 206 struct secpolicy *sp = NULL; 207 struct socket *so = inp ? inp->inp_socket : NULL; 208 #endif 209 #ifdef FAST_IPSEC 210 struct secpolicy *sp = NULL; 211 struct tdb_ident *tdbi; 212 #endif /* FAST_IPSEC */ 213 struct sockaddr_in *next_hop = NULL; 214 int src_was_INADDR_ANY = 0; /* as the name says... */ 215 216 m = m0; 217 M_ASSERTPKTHDR(m); 218 219 if (ro == NULL) { 220 ro = &iproute; 221 bzero(ro, sizeof *ro); 222 } else if (ro->ro_rt != NULL && ro->ro_rt->rt_cpuid != mycpuid) { 223 if (flags & IP_DEBUGROUTE) { 224 if (route_assert_owner_access) { 225 panic("ip_output: " 226 "rt rt_cpuid %d accessed on cpu %d\n", 227 ro->ro_rt->rt_cpuid, mycpuid); 228 } else { 229 kprintf("ip_output: " 230 "rt rt_cpuid %d accessed on cpu %d\n", 231 ro->ro_rt->rt_cpuid, mycpuid); 232 print_backtrace(-1); 233 } 234 } 235 236 /* 237 * XXX 238 * If the cached rtentry's owner CPU is not the current CPU, 239 * then don't touch the cached rtentry (remote free is too 240 * expensive in this context); just relocate the route. 241 */ 242 ro = &iproute; 243 bzero(ro, sizeof *ro); 244 } 245 246 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) { 247 /* Next hop */ 248 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 249 KKASSERT(mtag != NULL); 250 next_hop = m_tag_data(mtag); 251 } 252 253 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) { 254 struct dn_pkt *dn_pkt; 255 256 /* Extract info from dummynet tag */ 257 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 258 KKASSERT(mtag != NULL); 259 dn_pkt = m_tag_data(mtag); 260 261 /* 262 * The packet was already tagged, so part of the 263 * processing was already done, and we need to go down. 264 * Get the calculated parameters from the tag. 265 */ 266 ifp = dn_pkt->ifp; 267 268 KKASSERT(ro == &iproute); 269 *ro = dn_pkt->ro; /* structure copy */ 270 KKASSERT(ro->ro_rt == NULL || ro->ro_rt->rt_cpuid == mycpuid); 271 272 dst = dn_pkt->dn_dst; 273 if (dst == (struct sockaddr_in *)&(dn_pkt->ro.ro_dst)) { 274 /* If 'dst' points into dummynet tag, adjust it */ 275 dst = (struct sockaddr_in *)&(ro->ro_dst); 276 } 277 278 ip = mtod(m, struct ip *); 279 hlen = IP_VHL_HL(ip->ip_vhl) << 2 ; 280 if (ro->ro_rt) 281 ia = ifatoia(ro->ro_rt->rt_ifa); 282 goto sendit; 283 } 284 285 if (opt) { 286 len = 0; 287 m = ip_insertoptions(m, opt, &len); 288 if (len != 0) 289 hlen = len; 290 } 291 ip = mtod(m, struct ip *); 292 293 /* 294 * Fill in IP header. 295 */ 296 if (!(flags & (IP_FORWARDING|IP_RAWOUTPUT))) { 297 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2); 298 ip->ip_off &= IP_DF; 299 ip->ip_id = ip_newid(); 300 ipstat.ips_localout++; 301 } else { 302 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 303 } 304 305 reroute: 306 pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst; 307 308 #ifdef INVARIANTS 309 if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) { 310 /* 311 * XXX 312 * Multicast is not MPSAFE yet. Caller must hold 313 * BGL when output a multicast IP packet. 314 */ 315 ASSERT_MP_LOCK_HELD(curthread); 316 } 317 #endif 318 319 dst = (struct sockaddr_in *)&ro->ro_dst; 320 /* 321 * If there is a cached route, 322 * check that it is to the same destination 323 * and is still up. If not, free it and try again. 324 * The address family should also be checked in case of sharing the 325 * cache with IPv6. 326 */ 327 if (ro->ro_rt && 328 (!(ro->ro_rt->rt_flags & RTF_UP) || 329 dst->sin_family != AF_INET || 330 dst->sin_addr.s_addr != pkt_dst.s_addr)) { 331 rtfree(ro->ro_rt); 332 ro->ro_rt = NULL; 333 } 334 if (ro->ro_rt == NULL) { 335 bzero(dst, sizeof *dst); 336 dst->sin_family = AF_INET; 337 dst->sin_len = sizeof *dst; 338 dst->sin_addr = pkt_dst; 339 } 340 /* 341 * If routing to interface only, 342 * short circuit routing lookup. 343 */ 344 if (flags & IP_ROUTETOIF) { 345 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 346 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) { 347 ipstat.ips_noroute++; 348 error = ENETUNREACH; 349 goto bad; 350 } 351 ifp = ia->ia_ifp; 352 ip->ip_ttl = 1; 353 isbroadcast = in_broadcast(dst->sin_addr, ifp); 354 } else if (IN_MULTICAST(ntohl(pkt_dst.s_addr)) && 355 imo != NULL && imo->imo_multicast_ifp != NULL) { 356 /* 357 * Bypass the normal routing lookup for multicast 358 * packets if the interface is specified. 359 */ 360 ifp = imo->imo_multicast_ifp; 361 ia = IFP_TO_IA(ifp); 362 isbroadcast = 0; /* fool gcc */ 363 } else { 364 /* 365 * If this is the case, we probably don't want to allocate 366 * a protocol-cloned route since we didn't get one from the 367 * ULP. This lets TCP do its thing, while not burdening 368 * forwarding or ICMP with the overhead of cloning a route. 369 * Of course, we still want to do any cloning requested by 370 * the link layer, as this is probably required in all cases 371 * for correct operation (as it is for ARP). 372 */ 373 if (ro->ro_rt == NULL) 374 rtalloc_ign(ro, RTF_PRCLONING); 375 if (ro->ro_rt == NULL) { 376 ipstat.ips_noroute++; 377 error = EHOSTUNREACH; 378 goto bad; 379 } 380 ia = ifatoia(ro->ro_rt->rt_ifa); 381 ifp = ro->ro_rt->rt_ifp; 382 ro->ro_rt->rt_use++; 383 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 384 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 385 if (ro->ro_rt->rt_flags & RTF_HOST) 386 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 387 else 388 isbroadcast = in_broadcast(dst->sin_addr, ifp); 389 } 390 if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) { 391 struct in_multi *inm; 392 393 m->m_flags |= M_MCAST; 394 /* 395 * IP destination address is multicast. Make sure "dst" 396 * still points to the address in "ro". (It may have been 397 * changed to point to a gateway address, above.) 398 */ 399 dst = (struct sockaddr_in *)&ro->ro_dst; 400 /* 401 * See if the caller provided any multicast options 402 */ 403 if (imo != NULL) { 404 ip->ip_ttl = imo->imo_multicast_ttl; 405 if (imo->imo_multicast_vif != -1) { 406 ip->ip_src.s_addr = 407 ip_mcast_src ? 408 ip_mcast_src(imo->imo_multicast_vif) : 409 INADDR_ANY; 410 } 411 } else { 412 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 413 } 414 /* 415 * Confirm that the outgoing interface supports multicast. 416 */ 417 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 418 if (!(ifp->if_flags & IFF_MULTICAST)) { 419 ipstat.ips_noroute++; 420 error = ENETUNREACH; 421 goto bad; 422 } 423 } 424 /* 425 * If source address not specified yet, use address 426 * of outgoing interface. 427 */ 428 if (ip->ip_src.s_addr == INADDR_ANY) { 429 /* Interface may have no addresses. */ 430 if (ia != NULL) 431 ip->ip_src = IA_SIN(ia)->sin_addr; 432 } 433 434 IN_LOOKUP_MULTI(pkt_dst, ifp, inm); 435 if (inm != NULL && 436 (imo == NULL || imo->imo_multicast_loop)) { 437 /* 438 * If we belong to the destination multicast group 439 * on the outgoing interface, and the caller did not 440 * forbid loopback, loop back a copy. 441 */ 442 ip_mloopback(ifp, m, dst, hlen); 443 } else { 444 /* 445 * If we are acting as a multicast router, perform 446 * multicast forwarding as if the packet had just 447 * arrived on the interface to which we are about 448 * to send. The multicast forwarding function 449 * recursively calls this function, using the 450 * IP_FORWARDING flag to prevent infinite recursion. 451 * 452 * Multicasts that are looped back by ip_mloopback(), 453 * above, will be forwarded by the ip_input() routine, 454 * if necessary. 455 */ 456 if (ip_mrouter && !(flags & IP_FORWARDING)) { 457 /* 458 * If rsvp daemon is not running, do not 459 * set ip_moptions. This ensures that the packet 460 * is multicast and not just sent down one link 461 * as prescribed by rsvpd. 462 */ 463 if (!rsvp_on) 464 imo = NULL; 465 if (ip_mforward && 466 ip_mforward(ip, ifp, m, imo) != 0) { 467 m_freem(m); 468 goto done; 469 } 470 } 471 } 472 473 /* 474 * Multicasts with a time-to-live of zero may be looped- 475 * back, above, but must not be transmitted on a network. 476 * Also, multicasts addressed to the loopback interface 477 * are not sent -- the above call to ip_mloopback() will 478 * loop back a copy if this host actually belongs to the 479 * destination group on the loopback interface. 480 */ 481 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 482 m_freem(m); 483 goto done; 484 } 485 486 goto sendit; 487 } else { 488 m->m_flags &= ~M_MCAST; 489 } 490 491 /* 492 * If the source address is not specified yet, use the address 493 * of the outoing interface. In case, keep note we did that, so 494 * if the the firewall changes the next-hop causing the output 495 * interface to change, we can fix that. 496 */ 497 if (ip->ip_src.s_addr == INADDR_ANY || src_was_INADDR_ANY) { 498 /* Interface may have no addresses. */ 499 if (ia != NULL) { 500 ip->ip_src = IA_SIN(ia)->sin_addr; 501 src_was_INADDR_ANY = 1; 502 } 503 } 504 505 #ifdef ALTQ 506 /* 507 * Disable packet drop hack. 508 * Packetdrop should be done by queueing. 509 */ 510 #else /* !ALTQ */ 511 /* 512 * Verify that we have any chance at all of being able to queue 513 * the packet or packet fragments 514 */ 515 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 516 ifp->if_snd.ifq_maxlen) { 517 error = ENOBUFS; 518 ipstat.ips_odropped++; 519 goto bad; 520 } 521 #endif /* !ALTQ */ 522 523 /* 524 * Look for broadcast address and 525 * verify user is allowed to send 526 * such a packet. 527 */ 528 if (isbroadcast) { 529 if (!(ifp->if_flags & IFF_BROADCAST)) { 530 error = EADDRNOTAVAIL; 531 goto bad; 532 } 533 if (!(flags & IP_ALLOWBROADCAST)) { 534 error = EACCES; 535 goto bad; 536 } 537 /* don't allow broadcast messages to be fragmented */ 538 if (ip->ip_len > ifp->if_mtu) { 539 error = EMSGSIZE; 540 goto bad; 541 } 542 m->m_flags |= M_BCAST; 543 } else { 544 m->m_flags &= ~M_BCAST; 545 } 546 547 sendit: 548 #ifdef IPSEC 549 /* get SP for this packet */ 550 if (so == NULL) 551 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error); 552 else 553 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 554 555 if (sp == NULL) { 556 ipsecstat.out_inval++; 557 goto bad; 558 } 559 560 error = 0; 561 562 /* check policy */ 563 switch (sp->policy) { 564 case IPSEC_POLICY_DISCARD: 565 /* 566 * This packet is just discarded. 567 */ 568 ipsecstat.out_polvio++; 569 goto bad; 570 571 case IPSEC_POLICY_BYPASS: 572 case IPSEC_POLICY_NONE: 573 case IPSEC_POLICY_TCP: 574 /* no need to do IPsec. */ 575 goto skip_ipsec; 576 577 case IPSEC_POLICY_IPSEC: 578 if (sp->req == NULL) { 579 /* acquire a policy */ 580 error = key_spdacquire(sp); 581 goto bad; 582 } 583 break; 584 585 case IPSEC_POLICY_ENTRUST: 586 default: 587 kprintf("ip_output: Invalid policy found. %d\n", sp->policy); 588 } 589 { 590 struct ipsec_output_state state; 591 bzero(&state, sizeof state); 592 state.m = m; 593 if (flags & IP_ROUTETOIF) { 594 state.ro = &iproute; 595 bzero(&iproute, sizeof iproute); 596 } else 597 state.ro = ro; 598 state.dst = (struct sockaddr *)dst; 599 600 ip->ip_sum = 0; 601 602 /* 603 * XXX 604 * delayed checksums are not currently compatible with IPsec 605 */ 606 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 607 in_delayed_cksum(m); 608 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 609 } 610 611 ip->ip_len = htons(ip->ip_len); 612 ip->ip_off = htons(ip->ip_off); 613 614 error = ipsec4_output(&state, sp, flags); 615 616 m = state.m; 617 if (flags & IP_ROUTETOIF) { 618 /* 619 * if we have tunnel mode SA, we may need to ignore 620 * IP_ROUTETOIF. 621 */ 622 if (state.ro != &iproute || state.ro->ro_rt != NULL) { 623 flags &= ~IP_ROUTETOIF; 624 ro = state.ro; 625 } 626 } else 627 ro = state.ro; 628 dst = (struct sockaddr_in *)state.dst; 629 if (error) { 630 /* mbuf is already reclaimed in ipsec4_output. */ 631 m0 = NULL; 632 switch (error) { 633 case EHOSTUNREACH: 634 case ENETUNREACH: 635 case EMSGSIZE: 636 case ENOBUFS: 637 case ENOMEM: 638 break; 639 default: 640 kprintf("ip4_output (ipsec): error code %d\n", error); 641 /*fall through*/ 642 case ENOENT: 643 /* don't show these error codes to the user */ 644 error = 0; 645 break; 646 } 647 goto bad; 648 } 649 } 650 651 /* be sure to update variables that are affected by ipsec4_output() */ 652 ip = mtod(m, struct ip *); 653 #ifdef _IP_VHL 654 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 655 #else 656 hlen = ip->ip_hl << 2; 657 #endif 658 if (ro->ro_rt == NULL) { 659 if (!(flags & IP_ROUTETOIF)) { 660 kprintf("ip_output: " 661 "can't update route after IPsec processing\n"); 662 error = EHOSTUNREACH; /*XXX*/ 663 goto bad; 664 } 665 } else { 666 ia = ifatoia(ro->ro_rt->rt_ifa); 667 ifp = ro->ro_rt->rt_ifp; 668 } 669 670 /* make it flipped, again. */ 671 ip->ip_len = ntohs(ip->ip_len); 672 ip->ip_off = ntohs(ip->ip_off); 673 skip_ipsec: 674 #endif /*IPSEC*/ 675 #ifdef FAST_IPSEC 676 /* 677 * Check the security policy (SP) for the packet and, if 678 * required, do IPsec-related processing. There are two 679 * cases here; the first time a packet is sent through 680 * it will be untagged and handled by ipsec4_checkpolicy. 681 * If the packet is resubmitted to ip_output (e.g. after 682 * AH, ESP, etc. processing), there will be a tag to bypass 683 * the lookup and related policy checking. 684 */ 685 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL); 686 crit_enter(); 687 if (mtag != NULL) { 688 tdbi = (struct tdb_ident *)m_tag_data(mtag); 689 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND); 690 if (sp == NULL) 691 error = -EINVAL; /* force silent drop */ 692 m_tag_delete(m, mtag); 693 } else { 694 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, 695 &error, inp); 696 } 697 /* 698 * There are four return cases: 699 * sp != NULL apply IPsec policy 700 * sp == NULL, error == 0 no IPsec handling needed 701 * sp == NULL, error == -EINVAL discard packet w/o error 702 * sp == NULL, error != 0 discard packet, report error 703 */ 704 if (sp != NULL) { 705 /* Loop detection, check if ipsec processing already done */ 706 KASSERT(sp->req != NULL, ("ip_output: no ipsec request")); 707 for (mtag = m_tag_first(m); mtag != NULL; 708 mtag = m_tag_next(m, mtag)) { 709 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT) 710 continue; 711 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE && 712 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED) 713 continue; 714 /* 715 * Check if policy has an SA associated with it. 716 * This can happen when an SP has yet to acquire 717 * an SA; e.g. on first reference. If it occurs, 718 * then we let ipsec4_process_packet do its thing. 719 */ 720 if (sp->req->sav == NULL) 721 break; 722 tdbi = (struct tdb_ident *)m_tag_data(mtag); 723 if (tdbi->spi == sp->req->sav->spi && 724 tdbi->proto == sp->req->sav->sah->saidx.proto && 725 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst, 726 sizeof(union sockaddr_union)) == 0) { 727 /* 728 * No IPsec processing is needed, free 729 * reference to SP. 730 * 731 * NB: null pointer to avoid free at 732 * done: below. 733 */ 734 KEY_FREESP(&sp), sp = NULL; 735 crit_exit(); 736 goto spd_done; 737 } 738 } 739 740 /* 741 * Do delayed checksums now because we send before 742 * this is done in the normal processing path. 743 */ 744 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 745 in_delayed_cksum(m); 746 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 747 } 748 749 ip->ip_len = htons(ip->ip_len); 750 ip->ip_off = htons(ip->ip_off); 751 752 /* NB: callee frees mbuf */ 753 error = ipsec4_process_packet(m, sp->req, flags, 0); 754 /* 755 * Preserve KAME behaviour: ENOENT can be returned 756 * when an SA acquire is in progress. Don't propagate 757 * this to user-level; it confuses applications. 758 * 759 * XXX this will go away when the SADB is redone. 760 */ 761 if (error == ENOENT) 762 error = 0; 763 crit_exit(); 764 goto done; 765 } else { 766 crit_exit(); 767 768 if (error != 0) { 769 /* 770 * Hack: -EINVAL is used to signal that a packet 771 * should be silently discarded. This is typically 772 * because we asked key management for an SA and 773 * it was delayed (e.g. kicked up to IKE). 774 */ 775 if (error == -EINVAL) 776 error = 0; 777 goto bad; 778 } else { 779 /* No IPsec processing for this packet. */ 780 } 781 #ifdef notyet 782 /* 783 * If deferred crypto processing is needed, check that 784 * the interface supports it. 785 */ 786 mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL); 787 if (mtag != NULL && !(ifp->if_capenable & IFCAP_IPSEC)) { 788 /* notify IPsec to do its own crypto */ 789 ipsp_skipcrypto_unmark((struct tdb_ident *)m_tag_data(mtag)); 790 error = EHOSTUNREACH; 791 goto bad; 792 } 793 #endif 794 } 795 spd_done: 796 #endif /* FAST_IPSEC */ 797 798 /* We are already being fwd'd from a firewall. */ 799 if (next_hop != NULL) 800 goto pass; 801 802 /* No pfil hooks */ 803 if (!pfil_has_hooks(&inet_pfil_hook)) { 804 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) { 805 /* 806 * Strip dummynet tags from stranded packets 807 */ 808 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 809 KKASSERT(mtag != NULL); 810 m_tag_delete(m, mtag); 811 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED; 812 } 813 goto pass; 814 } 815 816 /* 817 * IpHack's section. 818 * - Xlate: translate packet's addr/port (NAT). 819 * - Firewall: deny/allow/etc. 820 * - Wrap: fake packet's addr/port <unimpl.> 821 * - Encapsulate: put it in another IP and send out. <unimp.> 822 */ 823 824 /* 825 * Run through list of hooks for output packets. 826 */ 827 error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT); 828 if (error != 0 || m == NULL) 829 goto done; 830 ip = mtod(m, struct ip *); 831 832 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) { 833 /* 834 * Check dst to make sure it is directly reachable on the 835 * interface we previously thought it was. 836 * If it isn't (which may be likely in some situations) we have 837 * to re-route it (ie, find a route for the next-hop and the 838 * associated interface) and set them here. This is nested 839 * forwarding which in most cases is undesirable, except where 840 * such control is nigh impossible. So we do it here. 841 * And I'm babbling. 842 */ 843 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 844 KKASSERT(mtag != NULL); 845 next_hop = m_tag_data(mtag); 846 847 /* 848 * Try local forwarding first 849 */ 850 if (ip_localforward(m, next_hop, hlen)) 851 goto done; 852 853 /* 854 * Relocate the route based on next_hop. 855 * If the current route is inp's cache, keep it untouched. 856 */ 857 if (ro == &iproute && ro->ro_rt != NULL) { 858 RTFREE(ro->ro_rt); 859 ro->ro_rt = NULL; 860 } 861 ro = &iproute; 862 bzero(ro, sizeof *ro); 863 864 /* 865 * Forwarding to broadcast address is not allowed. 866 * XXX Should we follow IP_ROUTETOIF? 867 */ 868 flags &= ~(IP_ALLOWBROADCAST | IP_ROUTETOIF); 869 870 /* We are doing forwarding now */ 871 flags |= IP_FORWARDING; 872 873 goto reroute; 874 } 875 876 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) { 877 struct dn_pkt *dn_pkt; 878 879 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 880 KKASSERT(mtag != NULL); 881 dn_pkt = m_tag_data(mtag); 882 883 /* 884 * Under certain cases it is not possible to recalculate 885 * 'ro' and 'dst', let alone 'flags', so just save them in 886 * dummynet tag and avoid the possible wrong reculcalation 887 * when we come back to ip_output() again. 888 * 889 * All other parameters have been already used and so they 890 * are not needed anymore. 891 * XXX if the ifp is deleted while a pkt is in dummynet, 892 * we are in trouble! (TODO use ifnet_detach_event) 893 * 894 * We need to copy *ro because for ICMP pkts (and maybe 895 * others) the caller passed a pointer into the stack; 896 * dst might also be a pointer into *ro so it needs to 897 * be updated. 898 */ 899 dn_pkt->ro = *ro; 900 if (ro->ro_rt) 901 ro->ro_rt->rt_refcnt++; 902 if (dst == (struct sockaddr_in *)&ro->ro_dst) { 903 /* 'dst' points into 'ro' */ 904 dst = (struct sockaddr_in *)&(dn_pkt->ro.ro_dst); 905 } 906 dn_pkt->dn_dst = dst; 907 dn_pkt->flags = flags; 908 909 ip_dn_queue(m); 910 goto done; 911 } 912 pass: 913 /* 127/8 must not appear on wire - RFC1122. */ 914 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 915 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 916 if (!(ifp->if_flags & IFF_LOOPBACK)) { 917 ipstat.ips_badaddr++; 918 error = EADDRNOTAVAIL; 919 goto bad; 920 } 921 } 922 923 m->m_pkthdr.csum_flags |= CSUM_IP; 924 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 925 if (sw_csum & CSUM_DELAY_DATA) { 926 in_delayed_cksum(m); 927 sw_csum &= ~CSUM_DELAY_DATA; 928 } 929 m->m_pkthdr.csum_flags &= ifp->if_hwassist; 930 931 /* 932 * If small enough for interface, or the interface will take 933 * care of the fragmentation for us, can just send directly. 934 */ 935 if (ip->ip_len <= ifp->if_mtu || ((ifp->if_hwassist & CSUM_FRAGMENT) && 936 !(ip->ip_off & IP_DF))) { 937 ip->ip_len = htons(ip->ip_len); 938 ip->ip_off = htons(ip->ip_off); 939 ip->ip_sum = 0; 940 if (sw_csum & CSUM_DELAY_IP) { 941 if (ip->ip_vhl == IP_VHL_BORING) 942 ip->ip_sum = in_cksum_hdr(ip); 943 else 944 ip->ip_sum = in_cksum(m, hlen); 945 } 946 947 /* Record statistics for this interface address. */ 948 if (!(flags & IP_FORWARDING) && ia) { 949 ia->ia_ifa.if_opackets++; 950 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 951 } 952 953 #ifdef IPSEC 954 /* clean ipsec history once it goes out of the node */ 955 ipsec_delaux(m); 956 #endif 957 958 #ifdef MBUF_STRESS_TEST 959 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) { 960 struct mbuf *m1, *m2; 961 int length, tmp; 962 963 tmp = length = m->m_pkthdr.len; 964 965 while ((length -= mbuf_frag_size) >= 1) { 966 m1 = m_split(m, length, MB_DONTWAIT); 967 if (m1 == NULL) 968 break; 969 m2 = m; 970 while (m2->m_next != NULL) 971 m2 = m2->m_next; 972 m2->m_next = m1; 973 } 974 m->m_pkthdr.len = tmp; 975 } 976 #endif 977 978 #ifdef MPLS 979 if (!mpls_output_process(m, ro->ro_rt)) 980 goto done; 981 #endif 982 error = ifp->if_output(ifp, m, (struct sockaddr *)dst, 983 ro->ro_rt); 984 goto done; 985 } 986 987 if (ip->ip_off & IP_DF) { 988 error = EMSGSIZE; 989 /* 990 * This case can happen if the user changed the MTU 991 * of an interface after enabling IP on it. Because 992 * most netifs don't keep track of routes pointing to 993 * them, there is no way for one to update all its 994 * routes when the MTU is changed. 995 */ 996 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) && 997 !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) && 998 (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 999 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 1000 } 1001 ipstat.ips_cantfrag++; 1002 goto bad; 1003 } 1004 1005 /* 1006 * Too large for interface; fragment if possible. If successful, 1007 * on return, m will point to a list of packets to be sent. 1008 */ 1009 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum); 1010 if (error) 1011 goto bad; 1012 for (; m; m = m0) { 1013 m0 = m->m_nextpkt; 1014 m->m_nextpkt = NULL; 1015 #ifdef IPSEC 1016 /* clean ipsec history once it goes out of the node */ 1017 ipsec_delaux(m); 1018 #endif 1019 if (error == 0) { 1020 /* Record statistics for this interface address. */ 1021 if (ia != NULL) { 1022 ia->ia_ifa.if_opackets++; 1023 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 1024 } 1025 #ifdef MPLS 1026 if (!mpls_output_process(m, ro->ro_rt)) 1027 continue; 1028 #endif 1029 error = ifp->if_output(ifp, m, (struct sockaddr *)dst, 1030 ro->ro_rt); 1031 } else { 1032 m_freem(m); 1033 } 1034 } 1035 1036 if (error == 0) 1037 ipstat.ips_fragmented++; 1038 1039 done: 1040 if (ro == &iproute && ro->ro_rt != NULL) { 1041 RTFREE(ro->ro_rt); 1042 ro->ro_rt = NULL; 1043 } 1044 #ifdef IPSEC 1045 if (sp != NULL) { 1046 KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 1047 kprintf("DP ip_output call free SP:%p\n", sp)); 1048 key_freesp(sp); 1049 } 1050 #endif 1051 #ifdef FAST_IPSEC 1052 if (sp != NULL) 1053 KEY_FREESP(&sp); 1054 #endif 1055 return (error); 1056 bad: 1057 m_freem(m); 1058 goto done; 1059 } 1060 1061 /* 1062 * Create a chain of fragments which fit the given mtu. m_frag points to the 1063 * mbuf to be fragmented; on return it points to the chain with the fragments. 1064 * Return 0 if no error. If error, m_frag may contain a partially built 1065 * chain of fragments that should be freed by the caller. 1066 * 1067 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 1068 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 1069 */ 1070 int 1071 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 1072 u_long if_hwassist_flags, int sw_csum) 1073 { 1074 int error = 0; 1075 int hlen = IP_VHL_HL(ip->ip_vhl) << 2; 1076 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 1077 int off; 1078 struct mbuf *m0 = *m_frag; /* the original packet */ 1079 int firstlen; 1080 struct mbuf **mnext; 1081 int nfrags; 1082 1083 if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */ 1084 ipstat.ips_cantfrag++; 1085 return EMSGSIZE; 1086 } 1087 1088 /* 1089 * Must be able to put at least 8 bytes per fragment. 1090 */ 1091 if (len < 8) 1092 return EMSGSIZE; 1093 1094 /* 1095 * If the interface will not calculate checksums on 1096 * fragmented packets, then do it here. 1097 */ 1098 if ((m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) && 1099 !(if_hwassist_flags & CSUM_IP_FRAGS)) { 1100 in_delayed_cksum(m0); 1101 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1102 } 1103 1104 if (len > PAGE_SIZE) { 1105 /* 1106 * Fragment large datagrams such that each segment 1107 * contains a multiple of PAGE_SIZE amount of data, 1108 * plus headers. This enables a receiver to perform 1109 * page-flipping zero-copy optimizations. 1110 * 1111 * XXX When does this help given that sender and receiver 1112 * could have different page sizes, and also mtu could 1113 * be less than the receiver's page size ? 1114 */ 1115 int newlen; 1116 struct mbuf *m; 1117 1118 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 1119 off += m->m_len; 1120 1121 /* 1122 * firstlen (off - hlen) must be aligned on an 1123 * 8-byte boundary 1124 */ 1125 if (off < hlen) 1126 goto smart_frag_failure; 1127 off = ((off - hlen) & ~7) + hlen; 1128 newlen = (~PAGE_MASK) & mtu; 1129 if ((newlen + sizeof(struct ip)) > mtu) { 1130 /* we failed, go back the default */ 1131 smart_frag_failure: 1132 newlen = len; 1133 off = hlen + len; 1134 } 1135 len = newlen; 1136 1137 } else { 1138 off = hlen + len; 1139 } 1140 1141 firstlen = off - hlen; 1142 mnext = &m0->m_nextpkt; /* pointer to next packet */ 1143 1144 /* 1145 * Loop through length of segment after first fragment, 1146 * make new header and copy data of each part and link onto chain. 1147 * Here, m0 is the original packet, m is the fragment being created. 1148 * The fragments are linked off the m_nextpkt of the original 1149 * packet, which after processing serves as the first fragment. 1150 */ 1151 for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) { 1152 struct ip *mhip; /* ip header on the fragment */ 1153 struct mbuf *m; 1154 int mhlen = sizeof(struct ip); 1155 1156 MGETHDR(m, MB_DONTWAIT, MT_HEADER); 1157 if (m == NULL) { 1158 error = ENOBUFS; 1159 ipstat.ips_odropped++; 1160 goto done; 1161 } 1162 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 1163 /* 1164 * In the first mbuf, leave room for the link header, then 1165 * copy the original IP header including options. The payload 1166 * goes into an additional mbuf chain returned by m_copy(). 1167 */ 1168 m->m_data += max_linkhdr; 1169 mhip = mtod(m, struct ip *); 1170 *mhip = *ip; 1171 if (hlen > sizeof(struct ip)) { 1172 mhlen = ip_optcopy(ip, mhip) + sizeof(struct ip); 1173 mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2); 1174 } 1175 m->m_len = mhlen; 1176 /* XXX do we need to add ip->ip_off below ? */ 1177 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 1178 if (off + len >= ip->ip_len) { /* last fragment */ 1179 len = ip->ip_len - off; 1180 m->m_flags |= M_LASTFRAG; 1181 } else 1182 mhip->ip_off |= IP_MF; 1183 mhip->ip_len = htons((u_short)(len + mhlen)); 1184 m->m_next = m_copy(m0, off, len); 1185 if (m->m_next == NULL) { /* copy failed */ 1186 m_free(m); 1187 error = ENOBUFS; /* ??? */ 1188 ipstat.ips_odropped++; 1189 goto done; 1190 } 1191 m->m_pkthdr.len = mhlen + len; 1192 m->m_pkthdr.rcvif = NULL; 1193 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 1194 mhip->ip_off = htons(mhip->ip_off); 1195 mhip->ip_sum = 0; 1196 if (sw_csum & CSUM_DELAY_IP) 1197 mhip->ip_sum = in_cksum(m, mhlen); 1198 *mnext = m; 1199 mnext = &m->m_nextpkt; 1200 } 1201 ipstat.ips_ofragments += nfrags; 1202 1203 /* set first marker for fragment chain */ 1204 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 1205 m0->m_pkthdr.csum_data = nfrags; 1206 1207 /* 1208 * Update first fragment by trimming what's been copied out 1209 * and updating header. 1210 */ 1211 m_adj(m0, hlen + firstlen - ip->ip_len); 1212 m0->m_pkthdr.len = hlen + firstlen; 1213 ip->ip_len = htons((u_short)m0->m_pkthdr.len); 1214 ip->ip_off |= IP_MF; 1215 ip->ip_off = htons(ip->ip_off); 1216 ip->ip_sum = 0; 1217 if (sw_csum & CSUM_DELAY_IP) 1218 ip->ip_sum = in_cksum(m0, hlen); 1219 1220 done: 1221 *m_frag = m0; 1222 return error; 1223 } 1224 1225 void 1226 in_delayed_cksum(struct mbuf *m) 1227 { 1228 struct ip *ip; 1229 u_short csum, offset; 1230 1231 ip = mtod(m, struct ip *); 1232 offset = IP_VHL_HL(ip->ip_vhl) << 2 ; 1233 csum = in_cksum_skip(m, ip->ip_len, offset); 1234 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 1235 csum = 0xffff; 1236 offset += m->m_pkthdr.csum_data; /* checksum offset */ 1237 1238 if (offset + sizeof(u_short) > m->m_len) { 1239 kprintf("delayed m_pullup, m->len: %d off: %d p: %d\n", 1240 m->m_len, offset, ip->ip_p); 1241 /* 1242 * XXX 1243 * this shouldn't happen, but if it does, the 1244 * correct behavior may be to insert the checksum 1245 * in the existing chain instead of rearranging it. 1246 */ 1247 m = m_pullup(m, offset + sizeof(u_short)); 1248 } 1249 *(u_short *)(m->m_data + offset) = csum; 1250 } 1251 1252 /* 1253 * Insert IP options into preformed packet. 1254 * Adjust IP destination as required for IP source routing, 1255 * as indicated by a non-zero in_addr at the start of the options. 1256 * 1257 * XXX This routine assumes that the packet has no options in place. 1258 */ 1259 static struct mbuf * 1260 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen) 1261 { 1262 struct ipoption *p = mtod(opt, struct ipoption *); 1263 struct mbuf *n; 1264 struct ip *ip = mtod(m, struct ip *); 1265 unsigned optlen; 1266 1267 optlen = opt->m_len - sizeof p->ipopt_dst; 1268 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) { 1269 *phlen = 0; 1270 return (m); /* XXX should fail */ 1271 } 1272 if (p->ipopt_dst.s_addr) 1273 ip->ip_dst = p->ipopt_dst; 1274 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 1275 MGETHDR(n, MB_DONTWAIT, MT_HEADER); 1276 if (n == NULL) { 1277 *phlen = 0; 1278 return (m); 1279 } 1280 n->m_pkthdr.rcvif = NULL; 1281 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 1282 m->m_len -= sizeof(struct ip); 1283 m->m_data += sizeof(struct ip); 1284 n->m_next = m; 1285 m = n; 1286 m->m_len = optlen + sizeof(struct ip); 1287 m->m_data += max_linkhdr; 1288 memcpy(mtod(m, void *), ip, sizeof(struct ip)); 1289 } else { 1290 m->m_data -= optlen; 1291 m->m_len += optlen; 1292 m->m_pkthdr.len += optlen; 1293 ovbcopy(ip, mtod(m, caddr_t), sizeof(struct ip)); 1294 } 1295 ip = mtod(m, struct ip *); 1296 bcopy(p->ipopt_list, ip + 1, optlen); 1297 *phlen = sizeof(struct ip) + optlen; 1298 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2); 1299 ip->ip_len += optlen; 1300 return (m); 1301 } 1302 1303 /* 1304 * Copy options from ip to jp, 1305 * omitting those not copied during fragmentation. 1306 */ 1307 int 1308 ip_optcopy(struct ip *ip, struct ip *jp) 1309 { 1310 u_char *cp, *dp; 1311 int opt, optlen, cnt; 1312 1313 cp = (u_char *)(ip + 1); 1314 dp = (u_char *)(jp + 1); 1315 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip); 1316 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1317 opt = cp[0]; 1318 if (opt == IPOPT_EOL) 1319 break; 1320 if (opt == IPOPT_NOP) { 1321 /* Preserve for IP mcast tunnel's LSRR alignment. */ 1322 *dp++ = IPOPT_NOP; 1323 optlen = 1; 1324 continue; 1325 } 1326 1327 KASSERT(cnt >= IPOPT_OLEN + sizeof *cp, 1328 ("ip_optcopy: malformed ipv4 option")); 1329 optlen = cp[IPOPT_OLEN]; 1330 KASSERT(optlen >= IPOPT_OLEN + sizeof *cp && optlen <= cnt, 1331 ("ip_optcopy: malformed ipv4 option")); 1332 1333 /* bogus lengths should have been caught by ip_dooptions */ 1334 if (optlen > cnt) 1335 optlen = cnt; 1336 if (IPOPT_COPIED(opt)) { 1337 bcopy(cp, dp, optlen); 1338 dp += optlen; 1339 } 1340 } 1341 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1342 *dp++ = IPOPT_EOL; 1343 return (optlen); 1344 } 1345 1346 /* 1347 * IP socket option processing. 1348 */ 1349 int 1350 ip_ctloutput(struct socket *so, struct sockopt *sopt) 1351 { 1352 struct inpcb *inp = so->so_pcb; 1353 int error, optval; 1354 1355 error = optval = 0; 1356 if (sopt->sopt_level != IPPROTO_IP) { 1357 return (EINVAL); 1358 } 1359 1360 switch (sopt->sopt_dir) { 1361 case SOPT_SET: 1362 switch (sopt->sopt_name) { 1363 case IP_OPTIONS: 1364 #ifdef notyet 1365 case IP_RETOPTS: 1366 #endif 1367 { 1368 struct mbuf *m; 1369 if (sopt->sopt_valsize > MLEN) { 1370 error = EMSGSIZE; 1371 break; 1372 } 1373 MGET(m, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_HEADER); 1374 if (m == NULL) { 1375 error = ENOBUFS; 1376 break; 1377 } 1378 m->m_len = sopt->sopt_valsize; 1379 error = soopt_to_kbuf(sopt, mtod(m, void *), m->m_len, 1380 m->m_len); 1381 return (ip_pcbopts(sopt->sopt_name, &inp->inp_options, 1382 m)); 1383 } 1384 1385 case IP_TOS: 1386 case IP_TTL: 1387 case IP_MINTTL: 1388 case IP_RECVOPTS: 1389 case IP_RECVRETOPTS: 1390 case IP_RECVDSTADDR: 1391 case IP_RECVIF: 1392 case IP_RECVTTL: 1393 case IP_FAITH: 1394 error = soopt_to_kbuf(sopt, &optval, sizeof optval, 1395 sizeof optval); 1396 if (error) 1397 break; 1398 switch (sopt->sopt_name) { 1399 case IP_TOS: 1400 inp->inp_ip_tos = optval; 1401 break; 1402 1403 case IP_TTL: 1404 inp->inp_ip_ttl = optval; 1405 break; 1406 case IP_MINTTL: 1407 if (optval >= 0 && optval <= MAXTTL) 1408 inp->inp_ip_minttl = optval; 1409 else 1410 error = EINVAL; 1411 break; 1412 #define OPTSET(bit) \ 1413 if (optval) \ 1414 inp->inp_flags |= bit; \ 1415 else \ 1416 inp->inp_flags &= ~bit; 1417 1418 case IP_RECVOPTS: 1419 OPTSET(INP_RECVOPTS); 1420 break; 1421 1422 case IP_RECVRETOPTS: 1423 OPTSET(INP_RECVRETOPTS); 1424 break; 1425 1426 case IP_RECVDSTADDR: 1427 OPTSET(INP_RECVDSTADDR); 1428 break; 1429 1430 case IP_RECVIF: 1431 OPTSET(INP_RECVIF); 1432 break; 1433 1434 case IP_RECVTTL: 1435 OPTSET(INP_RECVTTL); 1436 break; 1437 1438 case IP_FAITH: 1439 OPTSET(INP_FAITH); 1440 break; 1441 } 1442 break; 1443 #undef OPTSET 1444 1445 case IP_MULTICAST_IF: 1446 case IP_MULTICAST_VIF: 1447 case IP_MULTICAST_TTL: 1448 case IP_MULTICAST_LOOP: 1449 case IP_ADD_MEMBERSHIP: 1450 case IP_DROP_MEMBERSHIP: 1451 error = ip_setmoptions(sopt, &inp->inp_moptions); 1452 break; 1453 1454 case IP_PORTRANGE: 1455 error = soopt_to_kbuf(sopt, &optval, sizeof optval, 1456 sizeof optval); 1457 if (error) 1458 break; 1459 1460 switch (optval) { 1461 case IP_PORTRANGE_DEFAULT: 1462 inp->inp_flags &= ~(INP_LOWPORT); 1463 inp->inp_flags &= ~(INP_HIGHPORT); 1464 break; 1465 1466 case IP_PORTRANGE_HIGH: 1467 inp->inp_flags &= ~(INP_LOWPORT); 1468 inp->inp_flags |= INP_HIGHPORT; 1469 break; 1470 1471 case IP_PORTRANGE_LOW: 1472 inp->inp_flags &= ~(INP_HIGHPORT); 1473 inp->inp_flags |= INP_LOWPORT; 1474 break; 1475 1476 default: 1477 error = EINVAL; 1478 break; 1479 } 1480 break; 1481 1482 #if defined(IPSEC) || defined(FAST_IPSEC) 1483 case IP_IPSEC_POLICY: 1484 { 1485 caddr_t req; 1486 size_t len = 0; 1487 int priv; 1488 struct mbuf *m; 1489 int optname; 1490 1491 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1492 break; 1493 soopt_to_mbuf(sopt, m); 1494 priv = (sopt->sopt_td != NULL && 1495 priv_check(sopt->sopt_td, PRIV_ROOT) != 0) ? 0 : 1; 1496 req = mtod(m, caddr_t); 1497 len = m->m_len; 1498 optname = sopt->sopt_name; 1499 error = ipsec4_set_policy(inp, optname, req, len, priv); 1500 m_freem(m); 1501 break; 1502 } 1503 #endif /*IPSEC*/ 1504 1505 default: 1506 error = ENOPROTOOPT; 1507 break; 1508 } 1509 break; 1510 1511 case SOPT_GET: 1512 switch (sopt->sopt_name) { 1513 case IP_OPTIONS: 1514 case IP_RETOPTS: 1515 if (inp->inp_options) 1516 soopt_from_kbuf(sopt, mtod(inp->inp_options, 1517 char *), 1518 inp->inp_options->m_len); 1519 else 1520 sopt->sopt_valsize = 0; 1521 break; 1522 1523 case IP_TOS: 1524 case IP_TTL: 1525 case IP_MINTTL: 1526 case IP_RECVOPTS: 1527 case IP_RECVRETOPTS: 1528 case IP_RECVDSTADDR: 1529 case IP_RECVTTL: 1530 case IP_RECVIF: 1531 case IP_PORTRANGE: 1532 case IP_FAITH: 1533 switch (sopt->sopt_name) { 1534 1535 case IP_TOS: 1536 optval = inp->inp_ip_tos; 1537 break; 1538 1539 case IP_TTL: 1540 optval = inp->inp_ip_ttl; 1541 break; 1542 case IP_MINTTL: 1543 optval = inp->inp_ip_minttl; 1544 break; 1545 1546 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1547 1548 case IP_RECVOPTS: 1549 optval = OPTBIT(INP_RECVOPTS); 1550 break; 1551 1552 case IP_RECVRETOPTS: 1553 optval = OPTBIT(INP_RECVRETOPTS); 1554 break; 1555 1556 case IP_RECVDSTADDR: 1557 optval = OPTBIT(INP_RECVDSTADDR); 1558 break; 1559 1560 case IP_RECVTTL: 1561 optval = OPTBIT(INP_RECVTTL); 1562 break; 1563 1564 case IP_RECVIF: 1565 optval = OPTBIT(INP_RECVIF); 1566 break; 1567 1568 case IP_PORTRANGE: 1569 if (inp->inp_flags & INP_HIGHPORT) 1570 optval = IP_PORTRANGE_HIGH; 1571 else if (inp->inp_flags & INP_LOWPORT) 1572 optval = IP_PORTRANGE_LOW; 1573 else 1574 optval = 0; 1575 break; 1576 1577 case IP_FAITH: 1578 optval = OPTBIT(INP_FAITH); 1579 break; 1580 } 1581 soopt_from_kbuf(sopt, &optval, sizeof optval); 1582 break; 1583 1584 case IP_MULTICAST_IF: 1585 case IP_MULTICAST_VIF: 1586 case IP_MULTICAST_TTL: 1587 case IP_MULTICAST_LOOP: 1588 case IP_ADD_MEMBERSHIP: 1589 case IP_DROP_MEMBERSHIP: 1590 error = ip_getmoptions(sopt, inp->inp_moptions); 1591 break; 1592 1593 #if defined(IPSEC) || defined(FAST_IPSEC) 1594 case IP_IPSEC_POLICY: 1595 { 1596 struct mbuf *m = NULL; 1597 caddr_t req = NULL; 1598 size_t len = 0; 1599 1600 if (m != NULL) { 1601 req = mtod(m, caddr_t); 1602 len = m->m_len; 1603 } 1604 error = ipsec4_get_policy(so->so_pcb, req, len, &m); 1605 if (error == 0) 1606 error = soopt_from_mbuf(sopt, m); /* XXX */ 1607 if (error == 0) 1608 m_freem(m); 1609 break; 1610 } 1611 #endif /*IPSEC*/ 1612 1613 default: 1614 error = ENOPROTOOPT; 1615 break; 1616 } 1617 break; 1618 } 1619 return (error); 1620 } 1621 1622 /* 1623 * Set up IP options in pcb for insertion in output packets. 1624 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1625 * with destination address if source routed. 1626 */ 1627 static int 1628 ip_pcbopts(int optname, struct mbuf **pcbopt, struct mbuf *m) 1629 { 1630 int cnt, optlen; 1631 u_char *cp; 1632 u_char opt; 1633 1634 /* turn off any old options */ 1635 if (*pcbopt) 1636 m_free(*pcbopt); 1637 *pcbopt = 0; 1638 if (m == NULL || m->m_len == 0) { 1639 /* 1640 * Only turning off any previous options. 1641 */ 1642 if (m != NULL) 1643 m_free(m); 1644 return (0); 1645 } 1646 1647 if (m->m_len % sizeof(int32_t)) 1648 goto bad; 1649 /* 1650 * IP first-hop destination address will be stored before 1651 * actual options; move other options back 1652 * and clear it when none present. 1653 */ 1654 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1655 goto bad; 1656 cnt = m->m_len; 1657 m->m_len += sizeof(struct in_addr); 1658 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1659 ovbcopy(mtod(m, caddr_t), cp, cnt); 1660 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 1661 1662 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1663 opt = cp[IPOPT_OPTVAL]; 1664 if (opt == IPOPT_EOL) 1665 break; 1666 if (opt == IPOPT_NOP) 1667 optlen = 1; 1668 else { 1669 if (cnt < IPOPT_OLEN + sizeof *cp) 1670 goto bad; 1671 optlen = cp[IPOPT_OLEN]; 1672 if (optlen < IPOPT_OLEN + sizeof *cp || optlen > cnt) 1673 goto bad; 1674 } 1675 switch (opt) { 1676 1677 default: 1678 break; 1679 1680 case IPOPT_LSRR: 1681 case IPOPT_SSRR: 1682 /* 1683 * user process specifies route as: 1684 * ->A->B->C->D 1685 * D must be our final destination (but we can't 1686 * check that since we may not have connected yet). 1687 * A is first hop destination, which doesn't appear in 1688 * actual IP option, but is stored before the options. 1689 */ 1690 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1691 goto bad; 1692 m->m_len -= sizeof(struct in_addr); 1693 cnt -= sizeof(struct in_addr); 1694 optlen -= sizeof(struct in_addr); 1695 cp[IPOPT_OLEN] = optlen; 1696 /* 1697 * Move first hop before start of options. 1698 */ 1699 bcopy(&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1700 sizeof(struct in_addr)); 1701 /* 1702 * Then copy rest of options back 1703 * to close up the deleted entry. 1704 */ 1705 ovbcopy(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr), 1706 &cp[IPOPT_OFFSET+1], 1707 cnt - (IPOPT_MINOFF - 1)); 1708 break; 1709 } 1710 } 1711 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1712 goto bad; 1713 *pcbopt = m; 1714 return (0); 1715 1716 bad: 1717 m_free(m); 1718 return (EINVAL); 1719 } 1720 1721 /* 1722 * XXX 1723 * The whole multicast option thing needs to be re-thought. 1724 * Several of these options are equally applicable to non-multicast 1725 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1726 * standard option (IP_TTL). 1727 */ 1728 1729 /* 1730 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1731 */ 1732 static struct ifnet * 1733 ip_multicast_if(struct in_addr *a, int *ifindexp) 1734 { 1735 int ifindex; 1736 struct ifnet *ifp; 1737 1738 if (ifindexp) 1739 *ifindexp = 0; 1740 if (ntohl(a->s_addr) >> 24 == 0) { 1741 ifindex = ntohl(a->s_addr) & 0xffffff; 1742 if (ifindex < 0 || if_index < ifindex) 1743 return NULL; 1744 ifp = ifindex2ifnet[ifindex]; 1745 if (ifindexp) 1746 *ifindexp = ifindex; 1747 } else { 1748 ifp = INADDR_TO_IFP(a); 1749 } 1750 return ifp; 1751 } 1752 1753 /* 1754 * Set the IP multicast options in response to user setsockopt(). 1755 */ 1756 static int 1757 ip_setmoptions(struct sockopt *sopt, struct ip_moptions **imop) 1758 { 1759 int error = 0; 1760 int i; 1761 struct in_addr addr; 1762 struct ip_mreq mreq; 1763 struct ifnet *ifp; 1764 struct ip_moptions *imo = *imop; 1765 int ifindex; 1766 1767 if (imo == NULL) { 1768 /* 1769 * No multicast option buffer attached to the pcb; 1770 * allocate one and initialize to default values. 1771 */ 1772 imo = kmalloc(sizeof *imo, M_IPMOPTS, M_WAITOK); 1773 1774 *imop = imo; 1775 imo->imo_multicast_ifp = NULL; 1776 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1777 imo->imo_multicast_vif = -1; 1778 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1779 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1780 imo->imo_num_memberships = 0; 1781 } 1782 switch (sopt->sopt_name) { 1783 /* store an index number for the vif you wanna use in the send */ 1784 case IP_MULTICAST_VIF: 1785 if (legal_vif_num == 0) { 1786 error = EOPNOTSUPP; 1787 break; 1788 } 1789 error = soopt_to_kbuf(sopt, &i, sizeof i, sizeof i); 1790 if (error) 1791 break; 1792 if (!legal_vif_num(i) && (i != -1)) { 1793 error = EINVAL; 1794 break; 1795 } 1796 imo->imo_multicast_vif = i; 1797 break; 1798 1799 case IP_MULTICAST_IF: 1800 /* 1801 * Select the interface for outgoing multicast packets. 1802 */ 1803 error = soopt_to_kbuf(sopt, &addr, sizeof addr, sizeof addr); 1804 if (error) 1805 break; 1806 1807 /* 1808 * INADDR_ANY is used to remove a previous selection. 1809 * When no interface is selected, a default one is 1810 * chosen every time a multicast packet is sent. 1811 */ 1812 if (addr.s_addr == INADDR_ANY) { 1813 imo->imo_multicast_ifp = NULL; 1814 break; 1815 } 1816 /* 1817 * The selected interface is identified by its local 1818 * IP address. Find the interface and confirm that 1819 * it supports multicasting. 1820 */ 1821 crit_enter(); 1822 ifp = ip_multicast_if(&addr, &ifindex); 1823 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) { 1824 crit_exit(); 1825 error = EADDRNOTAVAIL; 1826 break; 1827 } 1828 imo->imo_multicast_ifp = ifp; 1829 if (ifindex) 1830 imo->imo_multicast_addr = addr; 1831 else 1832 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1833 crit_exit(); 1834 break; 1835 1836 case IP_MULTICAST_TTL: 1837 /* 1838 * Set the IP time-to-live for outgoing multicast packets. 1839 * The original multicast API required a char argument, 1840 * which is inconsistent with the rest of the socket API. 1841 * We allow either a char or an int. 1842 */ 1843 if (sopt->sopt_valsize == 1) { 1844 u_char ttl; 1845 error = soopt_to_kbuf(sopt, &ttl, 1, 1); 1846 if (error) 1847 break; 1848 imo->imo_multicast_ttl = ttl; 1849 } else { 1850 u_int ttl; 1851 error = soopt_to_kbuf(sopt, &ttl, sizeof ttl, sizeof ttl); 1852 if (error) 1853 break; 1854 if (ttl > 255) 1855 error = EINVAL; 1856 else 1857 imo->imo_multicast_ttl = ttl; 1858 } 1859 break; 1860 1861 case IP_MULTICAST_LOOP: 1862 /* 1863 * Set the loopback flag for outgoing multicast packets. 1864 * Must be zero or one. The original multicast API required a 1865 * char argument, which is inconsistent with the rest 1866 * of the socket API. We allow either a char or an int. 1867 */ 1868 if (sopt->sopt_valsize == 1) { 1869 u_char loop; 1870 1871 error = soopt_to_kbuf(sopt, &loop, 1, 1); 1872 if (error) 1873 break; 1874 imo->imo_multicast_loop = !!loop; 1875 } else { 1876 u_int loop; 1877 1878 error = soopt_to_kbuf(sopt, &loop, sizeof loop, 1879 sizeof loop); 1880 if (error) 1881 break; 1882 imo->imo_multicast_loop = !!loop; 1883 } 1884 break; 1885 1886 case IP_ADD_MEMBERSHIP: 1887 /* 1888 * Add a multicast group membership. 1889 * Group must be a valid IP multicast address. 1890 */ 1891 error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq); 1892 if (error) 1893 break; 1894 1895 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1896 error = EINVAL; 1897 break; 1898 } 1899 crit_enter(); 1900 /* 1901 * If no interface address was provided, use the interface of 1902 * the route to the given multicast address. 1903 */ 1904 if (mreq.imr_interface.s_addr == INADDR_ANY) { 1905 struct sockaddr_in dst; 1906 struct rtentry *rt; 1907 1908 bzero(&dst, sizeof(struct sockaddr_in)); 1909 dst.sin_len = sizeof(struct sockaddr_in); 1910 dst.sin_family = AF_INET; 1911 dst.sin_addr = mreq.imr_multiaddr; 1912 rt = rtlookup((struct sockaddr *)&dst); 1913 if (rt == NULL) { 1914 error = EADDRNOTAVAIL; 1915 crit_exit(); 1916 break; 1917 } 1918 --rt->rt_refcnt; 1919 ifp = rt->rt_ifp; 1920 } else { 1921 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1922 } 1923 1924 /* 1925 * See if we found an interface, and confirm that it 1926 * supports multicast. 1927 */ 1928 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) { 1929 error = EADDRNOTAVAIL; 1930 crit_exit(); 1931 break; 1932 } 1933 /* 1934 * See if the membership already exists or if all the 1935 * membership slots are full. 1936 */ 1937 for (i = 0; i < imo->imo_num_memberships; ++i) { 1938 if (imo->imo_membership[i]->inm_ifp == ifp && 1939 imo->imo_membership[i]->inm_addr.s_addr 1940 == mreq.imr_multiaddr.s_addr) 1941 break; 1942 } 1943 if (i < imo->imo_num_memberships) { 1944 error = EADDRINUSE; 1945 crit_exit(); 1946 break; 1947 } 1948 if (i == IP_MAX_MEMBERSHIPS) { 1949 error = ETOOMANYREFS; 1950 crit_exit(); 1951 break; 1952 } 1953 /* 1954 * Everything looks good; add a new record to the multicast 1955 * address list for the given interface. 1956 */ 1957 if ((imo->imo_membership[i] = 1958 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 1959 error = ENOBUFS; 1960 crit_exit(); 1961 break; 1962 } 1963 ++imo->imo_num_memberships; 1964 crit_exit(); 1965 break; 1966 1967 case IP_DROP_MEMBERSHIP: 1968 /* 1969 * Drop a multicast group membership. 1970 * Group must be a valid IP multicast address. 1971 */ 1972 error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq); 1973 if (error) 1974 break; 1975 1976 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1977 error = EINVAL; 1978 break; 1979 } 1980 1981 crit_enter(); 1982 /* 1983 * If an interface address was specified, get a pointer 1984 * to its ifnet structure. 1985 */ 1986 if (mreq.imr_interface.s_addr == INADDR_ANY) 1987 ifp = NULL; 1988 else { 1989 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1990 if (ifp == NULL) { 1991 error = EADDRNOTAVAIL; 1992 crit_exit(); 1993 break; 1994 } 1995 } 1996 /* 1997 * Find the membership in the membership array. 1998 */ 1999 for (i = 0; i < imo->imo_num_memberships; ++i) { 2000 if ((ifp == NULL || 2001 imo->imo_membership[i]->inm_ifp == ifp) && 2002 imo->imo_membership[i]->inm_addr.s_addr == 2003 mreq.imr_multiaddr.s_addr) 2004 break; 2005 } 2006 if (i == imo->imo_num_memberships) { 2007 error = EADDRNOTAVAIL; 2008 crit_exit(); 2009 break; 2010 } 2011 /* 2012 * Give up the multicast address record to which the 2013 * membership points. 2014 */ 2015 in_delmulti(imo->imo_membership[i]); 2016 /* 2017 * Remove the gap in the membership array. 2018 */ 2019 for (++i; i < imo->imo_num_memberships; ++i) 2020 imo->imo_membership[i-1] = imo->imo_membership[i]; 2021 --imo->imo_num_memberships; 2022 crit_exit(); 2023 break; 2024 2025 default: 2026 error = EOPNOTSUPP; 2027 break; 2028 } 2029 2030 /* 2031 * If all options have default values, no need to keep the mbuf. 2032 */ 2033 if (imo->imo_multicast_ifp == NULL && 2034 imo->imo_multicast_vif == -1 && 2035 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 2036 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 2037 imo->imo_num_memberships == 0) { 2038 kfree(*imop, M_IPMOPTS); 2039 *imop = NULL; 2040 } 2041 2042 return (error); 2043 } 2044 2045 /* 2046 * Return the IP multicast options in response to user getsockopt(). 2047 */ 2048 static int 2049 ip_getmoptions(struct sockopt *sopt, struct ip_moptions *imo) 2050 { 2051 struct in_addr addr; 2052 struct in_ifaddr *ia; 2053 int error, optval; 2054 u_char coptval; 2055 2056 error = 0; 2057 switch (sopt->sopt_name) { 2058 case IP_MULTICAST_VIF: 2059 if (imo != NULL) 2060 optval = imo->imo_multicast_vif; 2061 else 2062 optval = -1; 2063 soopt_from_kbuf(sopt, &optval, sizeof optval); 2064 break; 2065 2066 case IP_MULTICAST_IF: 2067 if (imo == NULL || imo->imo_multicast_ifp == NULL) 2068 addr.s_addr = INADDR_ANY; 2069 else if (imo->imo_multicast_addr.s_addr) { 2070 /* return the value user has set */ 2071 addr = imo->imo_multicast_addr; 2072 } else { 2073 ia = IFP_TO_IA(imo->imo_multicast_ifp); 2074 addr.s_addr = (ia == NULL) ? INADDR_ANY 2075 : IA_SIN(ia)->sin_addr.s_addr; 2076 } 2077 soopt_from_kbuf(sopt, &addr, sizeof addr); 2078 break; 2079 2080 case IP_MULTICAST_TTL: 2081 if (imo == NULL) 2082 optval = coptval = IP_DEFAULT_MULTICAST_TTL; 2083 else 2084 optval = coptval = imo->imo_multicast_ttl; 2085 if (sopt->sopt_valsize == 1) 2086 soopt_from_kbuf(sopt, &coptval, 1); 2087 else 2088 soopt_from_kbuf(sopt, &optval, sizeof optval); 2089 break; 2090 2091 case IP_MULTICAST_LOOP: 2092 if (imo == NULL) 2093 optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 2094 else 2095 optval = coptval = imo->imo_multicast_loop; 2096 if (sopt->sopt_valsize == 1) 2097 soopt_from_kbuf(sopt, &coptval, 1); 2098 else 2099 soopt_from_kbuf(sopt, &optval, sizeof optval); 2100 break; 2101 2102 default: 2103 error = ENOPROTOOPT; 2104 break; 2105 } 2106 return (error); 2107 } 2108 2109 /* 2110 * Discard the IP multicast options. 2111 */ 2112 void 2113 ip_freemoptions(struct ip_moptions *imo) 2114 { 2115 int i; 2116 2117 if (imo != NULL) { 2118 for (i = 0; i < imo->imo_num_memberships; ++i) 2119 in_delmulti(imo->imo_membership[i]); 2120 kfree(imo, M_IPMOPTS); 2121 } 2122 } 2123 2124 /* 2125 * Routine called from ip_output() to loop back a copy of an IP multicast 2126 * packet to the input queue of a specified interface. Note that this 2127 * calls the output routine of the loopback "driver", but with an interface 2128 * pointer that might NOT be a loopback interface -- evil, but easier than 2129 * replicating that code here. 2130 */ 2131 static void 2132 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 2133 int hlen) 2134 { 2135 struct ip *ip; 2136 struct mbuf *copym; 2137 2138 copym = m_copypacket(m, MB_DONTWAIT); 2139 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 2140 copym = m_pullup(copym, hlen); 2141 if (copym != NULL) { 2142 /* 2143 * if the checksum hasn't been computed, mark it as valid 2144 */ 2145 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 2146 in_delayed_cksum(copym); 2147 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 2148 copym->m_pkthdr.csum_flags |= 2149 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 2150 copym->m_pkthdr.csum_data = 0xffff; 2151 } 2152 /* 2153 * We don't bother to fragment if the IP length is greater 2154 * than the interface's MTU. Can this possibly matter? 2155 */ 2156 ip = mtod(copym, struct ip *); 2157 ip->ip_len = htons(ip->ip_len); 2158 ip->ip_off = htons(ip->ip_off); 2159 ip->ip_sum = 0; 2160 if (ip->ip_vhl == IP_VHL_BORING) { 2161 ip->ip_sum = in_cksum_hdr(ip); 2162 } else { 2163 ip->ip_sum = in_cksum(copym, hlen); 2164 } 2165 /* 2166 * NB: 2167 * It's not clear whether there are any lingering 2168 * reentrancy problems in other areas which might 2169 * be exposed by using ip_input directly (in 2170 * particular, everything which modifies the packet 2171 * in-place). Yet another option is using the 2172 * protosw directly to deliver the looped back 2173 * packet. For the moment, we'll err on the side 2174 * of safety by using if_simloop(). 2175 */ 2176 #if 1 /* XXX */ 2177 if (dst->sin_family != AF_INET) { 2178 kprintf("ip_mloopback: bad address family %d\n", 2179 dst->sin_family); 2180 dst->sin_family = AF_INET; 2181 } 2182 #endif 2183 if_simloop(ifp, copym, dst->sin_family, 0); 2184 } 2185 } 2186