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