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