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 int route_assert_owner_access; 121 122 extern struct protosw inetsw[]; 123 124 static int 125 ip_localforward(struct mbuf *m, const struct sockaddr_in *dst, int hlen) 126 { 127 struct in_ifaddr_container *iac; 128 129 /* 130 * We need to figure out if we have been forwarded to a local 131 * socket. If so, then we should somehow "loop back" to 132 * ip_input(), and get directed to the PCB as if we had received 133 * this packet. This is because it may be difficult to identify 134 * the packets you want to forward until they are being output 135 * and have selected an interface (e.g. locally initiated 136 * packets). If we used the loopback inteface, we would not be 137 * able to control what happens as the packet runs through 138 * ip_input() as it is done through a ISR. 139 */ 140 LIST_FOREACH(iac, INADDR_HASH(dst->sin_addr.s_addr), ia_hash) { 141 /* 142 * If the addr to forward to is one of ours, we pretend 143 * to be the destination for this packet. 144 */ 145 if (IA_SIN(iac->ia)->sin_addr.s_addr == dst->sin_addr.s_addr) 146 break; 147 } 148 if (iac != NULL) { 149 struct ip *ip; 150 151 if (m->m_pkthdr.rcvif == NULL) 152 m->m_pkthdr.rcvif = ifunit("lo0"); 153 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 154 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | 155 CSUM_PSEUDO_HDR; 156 m->m_pkthdr.csum_data = 0xffff; 157 } 158 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED | CSUM_IP_VALID; 159 160 /* 161 * Make sure that the IP header is in one mbuf, 162 * required by ip_input 163 */ 164 if (m->m_len < hlen) { 165 m = m_pullup(m, hlen); 166 if (m == NULL) { 167 /* The packet was freed; we are done */ 168 return 1; 169 } 170 } 171 ip = mtod(m, struct ip *); 172 173 ip->ip_len = htons(ip->ip_len); 174 ip->ip_off = htons(ip->ip_off); 175 ip_input(m); 176 177 return 1; /* The packet gets forwarded locally */ 178 } 179 return 0; 180 } 181 182 /* 183 * IP output. The packet in mbuf chain m contains a skeletal IP 184 * header (with len, off, ttl, proto, tos, src, dst). 185 * The mbuf chain containing the packet will be freed. 186 * The mbuf opt, if present, will not be freed. 187 */ 188 int 189 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, 190 int flags, struct ip_moptions *imo, struct inpcb *inp) 191 { 192 struct ip *ip; 193 struct ifnet *ifp = NULL; /* keep compiler happy */ 194 struct mbuf *m; 195 int hlen = sizeof(struct ip); 196 int len, error = 0; 197 struct sockaddr_in *dst = NULL; /* keep compiler happy */ 198 struct in_ifaddr *ia = NULL; 199 int isbroadcast, sw_csum; 200 struct in_addr pkt_dst; 201 struct route iproute; 202 struct m_tag *mtag; 203 #ifdef IPSEC 204 struct secpolicy *sp = NULL; 205 struct socket *so = inp ? inp->inp_socket : NULL; 206 #endif 207 #ifdef FAST_IPSEC 208 struct secpolicy *sp = NULL; 209 struct tdb_ident *tdbi; 210 #endif /* FAST_IPSEC */ 211 struct sockaddr_in *next_hop = NULL; 212 int src_was_INADDR_ANY = 0; /* as the name says... */ 213 214 m = m0; 215 M_ASSERTPKTHDR(m); 216 217 if (ro == NULL) { 218 ro = &iproute; 219 bzero(ro, sizeof *ro); 220 } else if (ro->ro_rt != NULL && ro->ro_rt->rt_cpuid != mycpuid) { 221 if (flags & IP_DEBUGROUTE) { 222 if (route_assert_owner_access) { 223 panic("ip_output: " 224 "rt rt_cpuid %d accessed on cpu %d\n", 225 ro->ro_rt->rt_cpuid, mycpuid); 226 } else { 227 kprintf("ip_output: " 228 "rt rt_cpuid %d accessed on cpu %d\n", 229 ro->ro_rt->rt_cpuid, mycpuid); 230 print_backtrace(-1); 231 } 232 } 233 234 /* 235 * XXX 236 * If the cached rtentry's owner CPU is not the current CPU, 237 * then don't touch the cached rtentry (remote free is too 238 * expensive in this context); just relocate the route. 239 */ 240 ro = &iproute; 241 bzero(ro, sizeof *ro); 242 } 243 244 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) { 245 /* Next hop */ 246 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 247 KKASSERT(mtag != NULL); 248 next_hop = m_tag_data(mtag); 249 } 250 251 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) { 252 struct dn_pkt *dn_pkt; 253 254 /* Extract info from dummynet tag */ 255 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); 256 KKASSERT(mtag != NULL); 257 dn_pkt = m_tag_data(mtag); 258 259 /* 260 * The packet was already tagged, so part of the 261 * processing was already done, and we need to go down. 262 * Get the calculated parameters from the tag. 263 */ 264 ifp = dn_pkt->ifp; 265 266 KKASSERT(ro == &iproute); 267 *ro = dn_pkt->ro; /* structure copy */ 268 KKASSERT(ro->ro_rt == NULL || ro->ro_rt->rt_cpuid == mycpuid); 269 270 dst = dn_pkt->dn_dst; 271 if (dst == (struct sockaddr_in *)&(dn_pkt->ro.ro_dst)) { 272 /* If 'dst' points into dummynet tag, adjust it */ 273 dst = (struct sockaddr_in *)&(ro->ro_dst); 274 } 275 276 ip = mtod(m, struct ip *); 277 hlen = IP_VHL_HL(ip->ip_vhl) << 2 ; 278 if (ro->ro_rt) 279 ia = ifatoia(ro->ro_rt->rt_ifa); 280 goto sendit; 281 } 282 283 if (opt) { 284 len = 0; 285 m = ip_insertoptions(m, opt, &len); 286 if (len != 0) 287 hlen = len; 288 } 289 ip = mtod(m, struct ip *); 290 291 /* 292 * Fill in IP header. 293 */ 294 if (!(flags & (IP_FORWARDING|IP_RAWOUTPUT))) { 295 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2); 296 ip->ip_off &= IP_DF; 297 ip->ip_id = ip_newid(); 298 ipstat.ips_localout++; 299 } else { 300 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 301 } 302 303 reroute: 304 pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst; 305 306 dst = (struct sockaddr_in *)&ro->ro_dst; 307 /* 308 * If there is a cached route, 309 * check that it is to the same destination 310 * and is still up. If not, free it and try again. 311 * The address family should also be checked in case of sharing the 312 * cache with IPv6. 313 */ 314 if (ro->ro_rt && 315 (!(ro->ro_rt->rt_flags & RTF_UP) || 316 dst->sin_family != AF_INET || 317 dst->sin_addr.s_addr != pkt_dst.s_addr)) { 318 rtfree(ro->ro_rt); 319 ro->ro_rt = NULL; 320 } 321 if (ro->ro_rt == NULL) { 322 bzero(dst, sizeof *dst); 323 dst->sin_family = AF_INET; 324 dst->sin_len = sizeof *dst; 325 dst->sin_addr = pkt_dst; 326 } 327 /* 328 * If routing to interface only, 329 * short circuit routing lookup. 330 */ 331 if (flags & IP_ROUTETOIF) { 332 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 333 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) { 334 ipstat.ips_noroute++; 335 error = ENETUNREACH; 336 goto bad; 337 } 338 ifp = ia->ia_ifp; 339 ip->ip_ttl = 1; 340 isbroadcast = in_broadcast(dst->sin_addr, ifp); 341 } else if (IN_MULTICAST(ntohl(pkt_dst.s_addr)) && 342 imo != NULL && imo->imo_multicast_ifp != NULL) { 343 /* 344 * Bypass the normal routing lookup for multicast 345 * packets if the interface is specified. 346 */ 347 ifp = imo->imo_multicast_ifp; 348 ia = IFP_TO_IA(ifp); 349 isbroadcast = 0; /* fool gcc */ 350 } else { 351 /* 352 * If this is the case, we probably don't want to allocate 353 * a protocol-cloned route since we didn't get one from the 354 * ULP. This lets TCP do its thing, while not burdening 355 * forwarding or ICMP with the overhead of cloning a route. 356 * Of course, we still want to do any cloning requested by 357 * the link layer, as this is probably required in all cases 358 * for correct operation (as it is for ARP). 359 */ 360 if (ro->ro_rt == NULL) 361 rtalloc_ign(ro, RTF_PRCLONING); 362 if (ro->ro_rt == NULL) { 363 ipstat.ips_noroute++; 364 error = EHOSTUNREACH; 365 goto bad; 366 } 367 ia = ifatoia(ro->ro_rt->rt_ifa); 368 ifp = ro->ro_rt->rt_ifp; 369 ro->ro_rt->rt_use++; 370 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 371 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 372 if (ro->ro_rt->rt_flags & RTF_HOST) 373 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 374 else 375 isbroadcast = in_broadcast(dst->sin_addr, ifp); 376 } 377 if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) { 378 struct in_multi *inm; 379 380 m->m_flags |= M_MCAST; 381 /* 382 * IP destination address is multicast. Make sure "dst" 383 * still points to the address in "ro". (It may have been 384 * changed to point to a gateway address, above.) 385 */ 386 dst = (struct sockaddr_in *)&ro->ro_dst; 387 /* 388 * See if the caller provided any multicast options 389 */ 390 if (imo != NULL) { 391 ip->ip_ttl = imo->imo_multicast_ttl; 392 if (imo->imo_multicast_vif != -1) { 393 ip->ip_src.s_addr = 394 ip_mcast_src ? 395 ip_mcast_src(imo->imo_multicast_vif) : 396 INADDR_ANY; 397 } 398 } else { 399 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 400 } 401 /* 402 * Confirm that the outgoing interface supports multicast. 403 */ 404 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 405 if (!(ifp->if_flags & IFF_MULTICAST)) { 406 ipstat.ips_noroute++; 407 error = ENETUNREACH; 408 goto bad; 409 } 410 } 411 /* 412 * If source address not specified yet, use address 413 * of outgoing interface. 414 */ 415 if (ip->ip_src.s_addr == INADDR_ANY) { 416 /* Interface may have no addresses. */ 417 if (ia != NULL) 418 ip->ip_src = IA_SIN(ia)->sin_addr; 419 } 420 421 IN_LOOKUP_MULTI(pkt_dst, ifp, inm); 422 if (inm != NULL && 423 (imo == NULL || imo->imo_multicast_loop)) { 424 /* 425 * If we belong to the destination multicast group 426 * on the outgoing interface, and the caller did not 427 * forbid loopback, loop back a copy. 428 */ 429 ip_mloopback(ifp, m, dst, hlen); 430 } else { 431 /* 432 * If we are acting as a multicast router, perform 433 * multicast forwarding as if the packet had just 434 * arrived on the interface to which we are about 435 * to send. The multicast forwarding function 436 * recursively calls this function, using the 437 * IP_FORWARDING flag to prevent infinite recursion. 438 * 439 * Multicasts that are looped back by ip_mloopback(), 440 * above, will be forwarded by the ip_input() routine, 441 * if necessary. 442 */ 443 if (ip_mrouter && !(flags & IP_FORWARDING)) { 444 /* 445 * If rsvp daemon is not running, do not 446 * set ip_moptions. This ensures that the packet 447 * is multicast and not just sent down one link 448 * as prescribed by rsvpd. 449 */ 450 if (!rsvp_on) 451 imo = NULL; 452 if (ip_mforward) { 453 get_mplock(); 454 if (ip_mforward(ip, ifp, m, imo) != 0) { 455 m_freem(m); 456 rel_mplock(); 457 goto done; 458 } 459 rel_mplock(); 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 outoing interface. In case, keep note we did that, so 485 * 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 896 if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) { 897 m->m_pkthdr.csum_flags |= CSUM_IP; 898 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 899 if (sw_csum & CSUM_DELAY_DATA) { 900 in_delayed_cksum(m); 901 sw_csum &= ~CSUM_DELAY_DATA; 902 } 903 m->m_pkthdr.csum_flags &= ifp->if_hwassist; 904 } else { 905 sw_csum = 0; 906 } 907 m->m_pkthdr.csum_iphlen = hlen; 908 909 /* 910 * If small enough for interface, or the interface will take 911 * care of the fragmentation or segmentation for us, can just 912 * send directly. 913 */ 914 if (ip->ip_len <= ifp->if_mtu || 915 ((ifp->if_hwassist & CSUM_FRAGMENT) && !(ip->ip_off & IP_DF)) || 916 (m->m_pkthdr.csum_flags & CSUM_TSO)) { 917 ip->ip_len = htons(ip->ip_len); 918 ip->ip_off = htons(ip->ip_off); 919 ip->ip_sum = 0; 920 if (sw_csum & CSUM_DELAY_IP) { 921 if (ip->ip_vhl == IP_VHL_BORING) 922 ip->ip_sum = in_cksum_hdr(ip); 923 else 924 ip->ip_sum = in_cksum(m, hlen); 925 } 926 927 /* Record statistics for this interface address. */ 928 if (!(flags & IP_FORWARDING) && ia) { 929 IFA_STAT_INC(&ia->ia_ifa, opackets, 1); 930 IFA_STAT_INC(&ia->ia_ifa, obytes, m->m_pkthdr.len); 931 } 932 933 #ifdef IPSEC 934 /* clean ipsec history once it goes out of the node */ 935 ipsec_delaux(m); 936 #endif 937 938 #ifdef MBUF_STRESS_TEST 939 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) { 940 struct mbuf *m1, *m2; 941 int length, tmp; 942 943 tmp = length = m->m_pkthdr.len; 944 945 while ((length -= mbuf_frag_size) >= 1) { 946 m1 = m_split(m, length, MB_DONTWAIT); 947 if (m1 == NULL) 948 break; 949 m2 = m; 950 while (m2->m_next != NULL) 951 m2 = m2->m_next; 952 m2->m_next = m1; 953 } 954 m->m_pkthdr.len = tmp; 955 } 956 #endif 957 958 #ifdef MPLS 959 if (!mpls_output_process(m, ro->ro_rt)) 960 goto done; 961 #endif 962 error = ifp->if_output(ifp, m, (struct sockaddr *)dst, 963 ro->ro_rt); 964 goto done; 965 } 966 967 if (ip->ip_off & IP_DF) { 968 error = EMSGSIZE; 969 /* 970 * This case can happen if the user changed the MTU 971 * of an interface after enabling IP on it. Because 972 * most netifs don't keep track of routes pointing to 973 * them, there is no way for one to update all its 974 * routes when the MTU is changed. 975 */ 976 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) && 977 !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) && 978 (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 979 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 980 } 981 ipstat.ips_cantfrag++; 982 goto bad; 983 } 984 985 /* 986 * Too large for interface; fragment if possible. If successful, 987 * on return, m will point to a list of packets to be sent. 988 */ 989 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum); 990 if (error) 991 goto bad; 992 for (; m; m = m0) { 993 m0 = m->m_nextpkt; 994 m->m_nextpkt = NULL; 995 #ifdef IPSEC 996 /* clean ipsec history once it goes out of the node */ 997 ipsec_delaux(m); 998 #endif 999 if (error == 0) { 1000 /* Record statistics for this interface address. */ 1001 if (ia != NULL) { 1002 IFA_STAT_INC(&ia->ia_ifa, opackets, 1); 1003 IFA_STAT_INC(&ia->ia_ifa, obytes, 1004 m->m_pkthdr.len); 1005 } 1006 #ifdef MPLS 1007 if (!mpls_output_process(m, ro->ro_rt)) 1008 continue; 1009 #endif 1010 error = ifp->if_output(ifp, m, (struct sockaddr *)dst, 1011 ro->ro_rt); 1012 } else { 1013 m_freem(m); 1014 } 1015 } 1016 1017 if (error == 0) 1018 ipstat.ips_fragmented++; 1019 1020 done: 1021 if (ro == &iproute && ro->ro_rt != NULL) { 1022 RTFREE(ro->ro_rt); 1023 ro->ro_rt = NULL; 1024 } 1025 #ifdef IPSEC 1026 if (sp != NULL) { 1027 KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 1028 kprintf("DP ip_output call free SP:%p\n", sp)); 1029 key_freesp(sp); 1030 } 1031 #endif 1032 #ifdef FAST_IPSEC 1033 if (sp != NULL) 1034 KEY_FREESP(&sp); 1035 #endif 1036 return (error); 1037 bad: 1038 m_freem(m); 1039 goto done; 1040 } 1041 1042 /* 1043 * Create a chain of fragments which fit the given mtu. m_frag points to the 1044 * mbuf to be fragmented; on return it points to the chain with the fragments. 1045 * Return 0 if no error. If error, m_frag may contain a partially built 1046 * chain of fragments that should be freed by the caller. 1047 * 1048 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 1049 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 1050 */ 1051 int 1052 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 1053 u_long if_hwassist_flags, int sw_csum) 1054 { 1055 int error = 0; 1056 int hlen = IP_VHL_HL(ip->ip_vhl) << 2; 1057 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 1058 int off; 1059 struct mbuf *m0 = *m_frag; /* the original packet */ 1060 int firstlen; 1061 struct mbuf **mnext; 1062 int nfrags; 1063 1064 if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */ 1065 ipstat.ips_cantfrag++; 1066 return EMSGSIZE; 1067 } 1068 1069 /* 1070 * Must be able to put at least 8 bytes per fragment. 1071 */ 1072 if (len < 8) 1073 return EMSGSIZE; 1074 1075 /* 1076 * If the interface will not calculate checksums on 1077 * fragmented packets, then do it here. 1078 */ 1079 if ((m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) && 1080 !(if_hwassist_flags & CSUM_IP_FRAGS)) { 1081 in_delayed_cksum(m0); 1082 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1083 } 1084 1085 if (len > PAGE_SIZE) { 1086 /* 1087 * Fragment large datagrams such that each segment 1088 * contains a multiple of PAGE_SIZE amount of data, 1089 * plus headers. This enables a receiver to perform 1090 * page-flipping zero-copy optimizations. 1091 * 1092 * XXX When does this help given that sender and receiver 1093 * could have different page sizes, and also mtu could 1094 * be less than the receiver's page size ? 1095 */ 1096 int newlen; 1097 struct mbuf *m; 1098 1099 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 1100 off += m->m_len; 1101 1102 /* 1103 * firstlen (off - hlen) must be aligned on an 1104 * 8-byte boundary 1105 */ 1106 if (off < hlen) 1107 goto smart_frag_failure; 1108 off = ((off - hlen) & ~7) + hlen; 1109 newlen = (~PAGE_MASK) & mtu; 1110 if ((newlen + sizeof(struct ip)) > mtu) { 1111 /* we failed, go back the default */ 1112 smart_frag_failure: 1113 newlen = len; 1114 off = hlen + len; 1115 } 1116 len = newlen; 1117 1118 } else { 1119 off = hlen + len; 1120 } 1121 1122 firstlen = off - hlen; 1123 mnext = &m0->m_nextpkt; /* pointer to next packet */ 1124 1125 /* 1126 * Loop through length of segment after first fragment, 1127 * make new header and copy data of each part and link onto chain. 1128 * Here, m0 is the original packet, m is the fragment being created. 1129 * The fragments are linked off the m_nextpkt of the original 1130 * packet, which after processing serves as the first fragment. 1131 */ 1132 for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) { 1133 struct ip *mhip; /* ip header on the fragment */ 1134 struct mbuf *m; 1135 int mhlen = sizeof(struct ip); 1136 1137 MGETHDR(m, MB_DONTWAIT, MT_HEADER); 1138 if (m == NULL) { 1139 error = ENOBUFS; 1140 ipstat.ips_odropped++; 1141 goto done; 1142 } 1143 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 1144 /* 1145 * In the first mbuf, leave room for the link header, then 1146 * copy the original IP header including options. The payload 1147 * goes into an additional mbuf chain returned by m_copy(). 1148 */ 1149 m->m_data += max_linkhdr; 1150 mhip = mtod(m, struct ip *); 1151 *mhip = *ip; 1152 if (hlen > sizeof(struct ip)) { 1153 mhlen = ip_optcopy(ip, mhip) + sizeof(struct ip); 1154 mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2); 1155 } 1156 m->m_len = mhlen; 1157 /* XXX do we need to add ip->ip_off below ? */ 1158 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 1159 if (off + len >= ip->ip_len) { /* last fragment */ 1160 len = ip->ip_len - off; 1161 m->m_flags |= M_LASTFRAG; 1162 } else 1163 mhip->ip_off |= IP_MF; 1164 mhip->ip_len = htons((u_short)(len + mhlen)); 1165 m->m_next = m_copy(m0, off, len); 1166 if (m->m_next == NULL) { /* copy failed */ 1167 m_free(m); 1168 error = ENOBUFS; /* ??? */ 1169 ipstat.ips_odropped++; 1170 goto done; 1171 } 1172 m->m_pkthdr.len = mhlen + len; 1173 m->m_pkthdr.rcvif = NULL; 1174 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 1175 m->m_pkthdr.csum_iphlen = mhlen; 1176 mhip->ip_off = htons(mhip->ip_off); 1177 mhip->ip_sum = 0; 1178 if (sw_csum & CSUM_DELAY_IP) 1179 mhip->ip_sum = in_cksum(m, mhlen); 1180 *mnext = m; 1181 mnext = &m->m_nextpkt; 1182 } 1183 ipstat.ips_ofragments += nfrags; 1184 1185 /* set first marker for fragment chain */ 1186 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 1187 m0->m_pkthdr.csum_data = nfrags; 1188 1189 /* 1190 * Update first fragment by trimming what's been copied out 1191 * and updating header. 1192 */ 1193 m_adj(m0, hlen + firstlen - ip->ip_len); 1194 m0->m_pkthdr.len = hlen + firstlen; 1195 ip->ip_len = htons((u_short)m0->m_pkthdr.len); 1196 ip->ip_off |= IP_MF; 1197 ip->ip_off = htons(ip->ip_off); 1198 ip->ip_sum = 0; 1199 if (sw_csum & CSUM_DELAY_IP) 1200 ip->ip_sum = in_cksum(m0, hlen); 1201 1202 done: 1203 *m_frag = m0; 1204 return error; 1205 } 1206 1207 void 1208 in_delayed_cksum(struct mbuf *m) 1209 { 1210 struct ip *ip; 1211 u_short csum, offset; 1212 1213 ip = mtod(m, struct ip *); 1214 offset = IP_VHL_HL(ip->ip_vhl) << 2 ; 1215 csum = in_cksum_skip(m, ip->ip_len, offset); 1216 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 1217 csum = 0xffff; 1218 offset += m->m_pkthdr.csum_data; /* checksum offset */ 1219 1220 if (offset + sizeof(u_short) > m->m_len) { 1221 kprintf("delayed m_pullup, m->len: %d off: %d p: %d\n", 1222 m->m_len, offset, ip->ip_p); 1223 /* 1224 * XXX 1225 * this shouldn't happen, but if it does, the 1226 * correct behavior may be to insert the checksum 1227 * in the existing chain instead of rearranging it. 1228 */ 1229 m = m_pullup(m, offset + sizeof(u_short)); 1230 } 1231 *(u_short *)(m->m_data + offset) = csum; 1232 } 1233 1234 /* 1235 * Insert IP options into preformed packet. 1236 * Adjust IP destination as required for IP source routing, 1237 * as indicated by a non-zero in_addr at the start of the options. 1238 * 1239 * XXX This routine assumes that the packet has no options in place. 1240 */ 1241 static struct mbuf * 1242 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen) 1243 { 1244 struct ipoption *p = mtod(opt, struct ipoption *); 1245 struct mbuf *n; 1246 struct ip *ip = mtod(m, struct ip *); 1247 unsigned optlen; 1248 1249 optlen = opt->m_len - sizeof p->ipopt_dst; 1250 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) { 1251 *phlen = 0; 1252 return (m); /* XXX should fail */ 1253 } 1254 if (p->ipopt_dst.s_addr) 1255 ip->ip_dst = p->ipopt_dst; 1256 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 1257 MGETHDR(n, MB_DONTWAIT, MT_HEADER); 1258 if (n == NULL) { 1259 *phlen = 0; 1260 return (m); 1261 } 1262 n->m_pkthdr.rcvif = NULL; 1263 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 1264 m->m_len -= sizeof(struct ip); 1265 m->m_data += sizeof(struct ip); 1266 n->m_next = m; 1267 m = n; 1268 m->m_len = optlen + sizeof(struct ip); 1269 m->m_data += max_linkhdr; 1270 memcpy(mtod(m, void *), ip, sizeof(struct ip)); 1271 } else { 1272 m->m_data -= optlen; 1273 m->m_len += optlen; 1274 m->m_pkthdr.len += optlen; 1275 ovbcopy(ip, mtod(m, caddr_t), sizeof(struct ip)); 1276 } 1277 ip = mtod(m, struct ip *); 1278 bcopy(p->ipopt_list, ip + 1, optlen); 1279 *phlen = sizeof(struct ip) + optlen; 1280 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2); 1281 ip->ip_len += optlen; 1282 return (m); 1283 } 1284 1285 /* 1286 * Copy options from ip to jp, 1287 * omitting those not copied during fragmentation. 1288 */ 1289 int 1290 ip_optcopy(struct ip *ip, struct ip *jp) 1291 { 1292 u_char *cp, *dp; 1293 int opt, optlen, cnt; 1294 1295 cp = (u_char *)(ip + 1); 1296 dp = (u_char *)(jp + 1); 1297 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip); 1298 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1299 opt = cp[0]; 1300 if (opt == IPOPT_EOL) 1301 break; 1302 if (opt == IPOPT_NOP) { 1303 /* Preserve for IP mcast tunnel's LSRR alignment. */ 1304 *dp++ = IPOPT_NOP; 1305 optlen = 1; 1306 continue; 1307 } 1308 1309 KASSERT(cnt >= IPOPT_OLEN + sizeof *cp, 1310 ("ip_optcopy: malformed ipv4 option")); 1311 optlen = cp[IPOPT_OLEN]; 1312 KASSERT(optlen >= IPOPT_OLEN + sizeof *cp && optlen <= cnt, 1313 ("ip_optcopy: malformed ipv4 option")); 1314 1315 /* bogus lengths should have been caught by ip_dooptions */ 1316 if (optlen > cnt) 1317 optlen = cnt; 1318 if (IPOPT_COPIED(opt)) { 1319 bcopy(cp, dp, optlen); 1320 dp += optlen; 1321 } 1322 } 1323 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1324 *dp++ = IPOPT_EOL; 1325 return (optlen); 1326 } 1327 1328 /* 1329 * IP socket option processing. 1330 */ 1331 void 1332 ip_ctloutput(netmsg_t msg) 1333 { 1334 struct socket *so = msg->base.nm_so; 1335 struct sockopt *sopt = msg->ctloutput.nm_sopt; 1336 struct inpcb *inp = so->so_pcb; 1337 int error, optval; 1338 1339 error = optval = 0; 1340 if (sopt->sopt_level != IPPROTO_IP) { 1341 error = EINVAL; 1342 goto done; 1343 } 1344 1345 switch (sopt->sopt_dir) { 1346 case SOPT_SET: 1347 switch (sopt->sopt_name) { 1348 case IP_OPTIONS: 1349 #ifdef notyet 1350 case IP_RETOPTS: 1351 #endif 1352 { 1353 struct mbuf *m; 1354 if (sopt->sopt_valsize > MLEN) { 1355 error = EMSGSIZE; 1356 break; 1357 } 1358 MGET(m, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_HEADER); 1359 if (m == NULL) { 1360 error = ENOBUFS; 1361 break; 1362 } 1363 m->m_len = sopt->sopt_valsize; 1364 error = soopt_to_kbuf(sopt, mtod(m, void *), m->m_len, 1365 m->m_len); 1366 error = ip_pcbopts(sopt->sopt_name, 1367 &inp->inp_options, m); 1368 goto done; 1369 } 1370 1371 case IP_TOS: 1372 case IP_TTL: 1373 case IP_MINTTL: 1374 case IP_RECVOPTS: 1375 case IP_RECVRETOPTS: 1376 case IP_RECVDSTADDR: 1377 case IP_RECVIF: 1378 case IP_RECVTTL: 1379 case IP_FAITH: 1380 error = soopt_to_kbuf(sopt, &optval, sizeof optval, 1381 sizeof optval); 1382 if (error) 1383 break; 1384 switch (sopt->sopt_name) { 1385 case IP_TOS: 1386 inp->inp_ip_tos = optval; 1387 break; 1388 1389 case IP_TTL: 1390 inp->inp_ip_ttl = optval; 1391 break; 1392 case IP_MINTTL: 1393 if (optval >= 0 && optval <= MAXTTL) 1394 inp->inp_ip_minttl = optval; 1395 else 1396 error = EINVAL; 1397 break; 1398 #define OPTSET(bit) \ 1399 if (optval) \ 1400 inp->inp_flags |= bit; \ 1401 else \ 1402 inp->inp_flags &= ~bit; 1403 1404 case IP_RECVOPTS: 1405 OPTSET(INP_RECVOPTS); 1406 break; 1407 1408 case IP_RECVRETOPTS: 1409 OPTSET(INP_RECVRETOPTS); 1410 break; 1411 1412 case IP_RECVDSTADDR: 1413 OPTSET(INP_RECVDSTADDR); 1414 break; 1415 1416 case IP_RECVIF: 1417 OPTSET(INP_RECVIF); 1418 break; 1419 1420 case IP_RECVTTL: 1421 OPTSET(INP_RECVTTL); 1422 break; 1423 1424 case IP_FAITH: 1425 OPTSET(INP_FAITH); 1426 break; 1427 } 1428 break; 1429 #undef OPTSET 1430 1431 case IP_MULTICAST_IF: 1432 case IP_MULTICAST_VIF: 1433 case IP_MULTICAST_TTL: 1434 case IP_MULTICAST_LOOP: 1435 case IP_ADD_MEMBERSHIP: 1436 case IP_DROP_MEMBERSHIP: 1437 error = ip_setmoptions(sopt, &inp->inp_moptions); 1438 break; 1439 1440 case IP_PORTRANGE: 1441 error = soopt_to_kbuf(sopt, &optval, sizeof optval, 1442 sizeof optval); 1443 if (error) 1444 break; 1445 1446 switch (optval) { 1447 case IP_PORTRANGE_DEFAULT: 1448 inp->inp_flags &= ~(INP_LOWPORT); 1449 inp->inp_flags &= ~(INP_HIGHPORT); 1450 break; 1451 1452 case IP_PORTRANGE_HIGH: 1453 inp->inp_flags &= ~(INP_LOWPORT); 1454 inp->inp_flags |= INP_HIGHPORT; 1455 break; 1456 1457 case IP_PORTRANGE_LOW: 1458 inp->inp_flags &= ~(INP_HIGHPORT); 1459 inp->inp_flags |= INP_LOWPORT; 1460 break; 1461 1462 default: 1463 error = EINVAL; 1464 break; 1465 } 1466 break; 1467 1468 #if defined(IPSEC) || defined(FAST_IPSEC) 1469 case IP_IPSEC_POLICY: 1470 { 1471 caddr_t req; 1472 size_t len = 0; 1473 int priv; 1474 struct mbuf *m; 1475 int optname; 1476 1477 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1478 break; 1479 soopt_to_mbuf(sopt, m); 1480 priv = (sopt->sopt_td != NULL && 1481 priv_check(sopt->sopt_td, PRIV_ROOT) != 0) ? 0 : 1; 1482 req = mtod(m, caddr_t); 1483 len = m->m_len; 1484 optname = sopt->sopt_name; 1485 error = ipsec4_set_policy(inp, optname, req, len, priv); 1486 m_freem(m); 1487 break; 1488 } 1489 #endif /*IPSEC*/ 1490 1491 default: 1492 error = ENOPROTOOPT; 1493 break; 1494 } 1495 break; 1496 1497 case SOPT_GET: 1498 switch (sopt->sopt_name) { 1499 case IP_OPTIONS: 1500 case IP_RETOPTS: 1501 if (inp->inp_options) 1502 soopt_from_kbuf(sopt, mtod(inp->inp_options, 1503 char *), 1504 inp->inp_options->m_len); 1505 else 1506 sopt->sopt_valsize = 0; 1507 break; 1508 1509 case IP_TOS: 1510 case IP_TTL: 1511 case IP_MINTTL: 1512 case IP_RECVOPTS: 1513 case IP_RECVRETOPTS: 1514 case IP_RECVDSTADDR: 1515 case IP_RECVTTL: 1516 case IP_RECVIF: 1517 case IP_PORTRANGE: 1518 case IP_FAITH: 1519 switch (sopt->sopt_name) { 1520 1521 case IP_TOS: 1522 optval = inp->inp_ip_tos; 1523 break; 1524 1525 case IP_TTL: 1526 optval = inp->inp_ip_ttl; 1527 break; 1528 case IP_MINTTL: 1529 optval = inp->inp_ip_minttl; 1530 break; 1531 1532 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1533 1534 case IP_RECVOPTS: 1535 optval = OPTBIT(INP_RECVOPTS); 1536 break; 1537 1538 case IP_RECVRETOPTS: 1539 optval = OPTBIT(INP_RECVRETOPTS); 1540 break; 1541 1542 case IP_RECVDSTADDR: 1543 optval = OPTBIT(INP_RECVDSTADDR); 1544 break; 1545 1546 case IP_RECVTTL: 1547 optval = OPTBIT(INP_RECVTTL); 1548 break; 1549 1550 case IP_RECVIF: 1551 optval = OPTBIT(INP_RECVIF); 1552 break; 1553 1554 case IP_PORTRANGE: 1555 if (inp->inp_flags & INP_HIGHPORT) 1556 optval = IP_PORTRANGE_HIGH; 1557 else if (inp->inp_flags & INP_LOWPORT) 1558 optval = IP_PORTRANGE_LOW; 1559 else 1560 optval = 0; 1561 break; 1562 1563 case IP_FAITH: 1564 optval = OPTBIT(INP_FAITH); 1565 break; 1566 } 1567 soopt_from_kbuf(sopt, &optval, sizeof optval); 1568 break; 1569 1570 case IP_MULTICAST_IF: 1571 case IP_MULTICAST_VIF: 1572 case IP_MULTICAST_TTL: 1573 case IP_MULTICAST_LOOP: 1574 case IP_ADD_MEMBERSHIP: 1575 case IP_DROP_MEMBERSHIP: 1576 error = ip_getmoptions(sopt, inp->inp_moptions); 1577 break; 1578 1579 #if defined(IPSEC) || defined(FAST_IPSEC) 1580 case IP_IPSEC_POLICY: 1581 { 1582 struct mbuf *m = NULL; 1583 caddr_t req = NULL; 1584 size_t len = 0; 1585 1586 if (m != NULL) { 1587 req = mtod(m, caddr_t); 1588 len = m->m_len; 1589 } 1590 error = ipsec4_get_policy(so->so_pcb, req, len, &m); 1591 if (error == 0) 1592 error = soopt_from_mbuf(sopt, m); /* XXX */ 1593 if (error == 0) 1594 m_freem(m); 1595 break; 1596 } 1597 #endif /*IPSEC*/ 1598 1599 default: 1600 error = ENOPROTOOPT; 1601 break; 1602 } 1603 break; 1604 } 1605 done: 1606 lwkt_replymsg(&msg->lmsg, error); 1607 } 1608 1609 /* 1610 * Set up IP options in pcb for insertion in output packets. 1611 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1612 * with destination address if source routed. 1613 */ 1614 static int 1615 ip_pcbopts(int optname, struct mbuf **pcbopt, struct mbuf *m) 1616 { 1617 int cnt, optlen; 1618 u_char *cp; 1619 u_char opt; 1620 1621 /* turn off any old options */ 1622 if (*pcbopt) 1623 m_free(*pcbopt); 1624 *pcbopt = NULL; 1625 if (m == NULL || m->m_len == 0) { 1626 /* 1627 * Only turning off any previous options. 1628 */ 1629 if (m != NULL) 1630 m_free(m); 1631 return (0); 1632 } 1633 1634 if (m->m_len % sizeof(int32_t)) 1635 goto bad; 1636 /* 1637 * IP first-hop destination address will be stored before 1638 * actual options; move other options back 1639 * and clear it when none present. 1640 */ 1641 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1642 goto bad; 1643 cnt = m->m_len; 1644 m->m_len += sizeof(struct in_addr); 1645 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1646 ovbcopy(mtod(m, caddr_t), cp, cnt); 1647 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 1648 1649 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1650 opt = cp[IPOPT_OPTVAL]; 1651 if (opt == IPOPT_EOL) 1652 break; 1653 if (opt == IPOPT_NOP) 1654 optlen = 1; 1655 else { 1656 if (cnt < IPOPT_OLEN + sizeof *cp) 1657 goto bad; 1658 optlen = cp[IPOPT_OLEN]; 1659 if (optlen < IPOPT_OLEN + sizeof *cp || optlen > cnt) 1660 goto bad; 1661 } 1662 switch (opt) { 1663 1664 default: 1665 break; 1666 1667 case IPOPT_LSRR: 1668 case IPOPT_SSRR: 1669 /* 1670 * user process specifies route as: 1671 * ->A->B->C->D 1672 * D must be our final destination (but we can't 1673 * check that since we may not have connected yet). 1674 * A is first hop destination, which doesn't appear in 1675 * actual IP option, but is stored before the options. 1676 */ 1677 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1678 goto bad; 1679 m->m_len -= sizeof(struct in_addr); 1680 cnt -= sizeof(struct in_addr); 1681 optlen -= sizeof(struct in_addr); 1682 cp[IPOPT_OLEN] = optlen; 1683 /* 1684 * Move first hop before start of options. 1685 */ 1686 bcopy(&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1687 sizeof(struct in_addr)); 1688 /* 1689 * Then copy rest of options back 1690 * to close up the deleted entry. 1691 */ 1692 ovbcopy(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr), 1693 &cp[IPOPT_OFFSET+1], 1694 cnt - (IPOPT_MINOFF - 1)); 1695 break; 1696 } 1697 } 1698 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1699 goto bad; 1700 *pcbopt = m; 1701 return (0); 1702 1703 bad: 1704 m_free(m); 1705 return (EINVAL); 1706 } 1707 1708 /* 1709 * XXX 1710 * The whole multicast option thing needs to be re-thought. 1711 * Several of these options are equally applicable to non-multicast 1712 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1713 * standard option (IP_TTL). 1714 */ 1715 1716 /* 1717 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1718 */ 1719 static struct ifnet * 1720 ip_multicast_if(struct in_addr *a, int *ifindexp) 1721 { 1722 int ifindex; 1723 struct ifnet *ifp; 1724 1725 if (ifindexp) 1726 *ifindexp = 0; 1727 if (ntohl(a->s_addr) >> 24 == 0) { 1728 ifindex = ntohl(a->s_addr) & 0xffffff; 1729 if (ifindex < 0 || if_index < ifindex) 1730 return NULL; 1731 ifp = ifindex2ifnet[ifindex]; 1732 if (ifindexp) 1733 *ifindexp = ifindex; 1734 } else { 1735 ifp = INADDR_TO_IFP(a); 1736 } 1737 return ifp; 1738 } 1739 1740 /* 1741 * Set the IP multicast options in response to user setsockopt(). 1742 */ 1743 static int 1744 ip_setmoptions(struct sockopt *sopt, struct ip_moptions **imop) 1745 { 1746 int error = 0; 1747 int i; 1748 struct in_addr addr; 1749 struct ip_mreq mreq; 1750 struct ifnet *ifp; 1751 struct ip_moptions *imo = *imop; 1752 int ifindex; 1753 1754 if (imo == NULL) { 1755 /* 1756 * No multicast option buffer attached to the pcb; 1757 * allocate one and initialize to default values. 1758 */ 1759 imo = kmalloc(sizeof *imo, M_IPMOPTS, M_WAITOK); 1760 1761 *imop = imo; 1762 imo->imo_multicast_ifp = NULL; 1763 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1764 imo->imo_multicast_vif = -1; 1765 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1766 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1767 imo->imo_num_memberships = 0; 1768 } 1769 switch (sopt->sopt_name) { 1770 /* store an index number for the vif you wanna use in the send */ 1771 case IP_MULTICAST_VIF: 1772 if (legal_vif_num == 0) { 1773 error = EOPNOTSUPP; 1774 break; 1775 } 1776 error = soopt_to_kbuf(sopt, &i, sizeof i, sizeof i); 1777 if (error) 1778 break; 1779 if (!legal_vif_num(i) && (i != -1)) { 1780 error = EINVAL; 1781 break; 1782 } 1783 imo->imo_multicast_vif = i; 1784 break; 1785 1786 case IP_MULTICAST_IF: 1787 /* 1788 * Select the interface for outgoing multicast packets. 1789 */ 1790 error = soopt_to_kbuf(sopt, &addr, sizeof addr, sizeof addr); 1791 if (error) 1792 break; 1793 1794 /* 1795 * INADDR_ANY is used to remove a previous selection. 1796 * When no interface is selected, a default one is 1797 * chosen every time a multicast packet is sent. 1798 */ 1799 if (addr.s_addr == INADDR_ANY) { 1800 imo->imo_multicast_ifp = NULL; 1801 break; 1802 } 1803 /* 1804 * The selected interface is identified by its local 1805 * IP address. Find the interface and confirm that 1806 * it supports multicasting. 1807 */ 1808 crit_enter(); 1809 ifp = ip_multicast_if(&addr, &ifindex); 1810 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) { 1811 crit_exit(); 1812 error = EADDRNOTAVAIL; 1813 break; 1814 } 1815 imo->imo_multicast_ifp = ifp; 1816 if (ifindex) 1817 imo->imo_multicast_addr = addr; 1818 else 1819 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1820 crit_exit(); 1821 break; 1822 1823 case IP_MULTICAST_TTL: 1824 /* 1825 * Set the IP time-to-live for outgoing multicast packets. 1826 * The original multicast API required a char argument, 1827 * which is inconsistent with the rest of the socket API. 1828 * We allow either a char or an int. 1829 */ 1830 if (sopt->sopt_valsize == 1) { 1831 u_char ttl; 1832 error = soopt_to_kbuf(sopt, &ttl, 1, 1); 1833 if (error) 1834 break; 1835 imo->imo_multicast_ttl = ttl; 1836 } else { 1837 u_int ttl; 1838 error = soopt_to_kbuf(sopt, &ttl, sizeof ttl, sizeof ttl); 1839 if (error) 1840 break; 1841 if (ttl > 255) 1842 error = EINVAL; 1843 else 1844 imo->imo_multicast_ttl = ttl; 1845 } 1846 break; 1847 1848 case IP_MULTICAST_LOOP: 1849 /* 1850 * Set the loopback flag for outgoing multicast packets. 1851 * Must be zero or one. The original multicast API required a 1852 * char argument, which is inconsistent with the rest 1853 * of the socket API. We allow either a char or an int. 1854 */ 1855 if (sopt->sopt_valsize == 1) { 1856 u_char loop; 1857 1858 error = soopt_to_kbuf(sopt, &loop, 1, 1); 1859 if (error) 1860 break; 1861 imo->imo_multicast_loop = !!loop; 1862 } else { 1863 u_int loop; 1864 1865 error = soopt_to_kbuf(sopt, &loop, sizeof loop, 1866 sizeof loop); 1867 if (error) 1868 break; 1869 imo->imo_multicast_loop = !!loop; 1870 } 1871 break; 1872 1873 case IP_ADD_MEMBERSHIP: 1874 /* 1875 * Add a multicast group membership. 1876 * Group must be a valid IP multicast address. 1877 */ 1878 error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq); 1879 if (error) 1880 break; 1881 1882 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1883 error = EINVAL; 1884 break; 1885 } 1886 crit_enter(); 1887 /* 1888 * If no interface address was provided, use the interface of 1889 * the route to the given multicast address. 1890 */ 1891 if (mreq.imr_interface.s_addr == INADDR_ANY) { 1892 struct sockaddr_in dst; 1893 struct rtentry *rt; 1894 1895 bzero(&dst, sizeof(struct sockaddr_in)); 1896 dst.sin_len = sizeof(struct sockaddr_in); 1897 dst.sin_family = AF_INET; 1898 dst.sin_addr = mreq.imr_multiaddr; 1899 rt = rtlookup((struct sockaddr *)&dst); 1900 if (rt == NULL) { 1901 error = EADDRNOTAVAIL; 1902 crit_exit(); 1903 break; 1904 } 1905 --rt->rt_refcnt; 1906 ifp = rt->rt_ifp; 1907 } else { 1908 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1909 } 1910 1911 /* 1912 * See if we found an interface, and confirm that it 1913 * supports multicast. 1914 */ 1915 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) { 1916 error = EADDRNOTAVAIL; 1917 crit_exit(); 1918 break; 1919 } 1920 /* 1921 * See if the membership already exists or if all the 1922 * membership slots are full. 1923 */ 1924 for (i = 0; i < imo->imo_num_memberships; ++i) { 1925 if (imo->imo_membership[i]->inm_ifp == ifp && 1926 imo->imo_membership[i]->inm_addr.s_addr 1927 == mreq.imr_multiaddr.s_addr) 1928 break; 1929 } 1930 if (i < imo->imo_num_memberships) { 1931 error = EADDRINUSE; 1932 crit_exit(); 1933 break; 1934 } 1935 if (i == IP_MAX_MEMBERSHIPS) { 1936 error = ETOOMANYREFS; 1937 crit_exit(); 1938 break; 1939 } 1940 /* 1941 * Everything looks good; add a new record to the multicast 1942 * address list for the given interface. 1943 */ 1944 if ((imo->imo_membership[i] = 1945 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 1946 error = ENOBUFS; 1947 crit_exit(); 1948 break; 1949 } 1950 ++imo->imo_num_memberships; 1951 crit_exit(); 1952 break; 1953 1954 case IP_DROP_MEMBERSHIP: 1955 /* 1956 * Drop a multicast group membership. 1957 * Group must be a valid IP multicast address. 1958 */ 1959 error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq); 1960 if (error) 1961 break; 1962 1963 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1964 error = EINVAL; 1965 break; 1966 } 1967 1968 crit_enter(); 1969 /* 1970 * If an interface address was specified, get a pointer 1971 * to its ifnet structure. 1972 */ 1973 if (mreq.imr_interface.s_addr == INADDR_ANY) 1974 ifp = NULL; 1975 else { 1976 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1977 if (ifp == NULL) { 1978 error = EADDRNOTAVAIL; 1979 crit_exit(); 1980 break; 1981 } 1982 } 1983 /* 1984 * Find the membership in the membership array. 1985 */ 1986 for (i = 0; i < imo->imo_num_memberships; ++i) { 1987 if ((ifp == NULL || 1988 imo->imo_membership[i]->inm_ifp == ifp) && 1989 imo->imo_membership[i]->inm_addr.s_addr == 1990 mreq.imr_multiaddr.s_addr) 1991 break; 1992 } 1993 if (i == imo->imo_num_memberships) { 1994 error = EADDRNOTAVAIL; 1995 crit_exit(); 1996 break; 1997 } 1998 /* 1999 * Give up the multicast address record to which the 2000 * membership points. 2001 */ 2002 in_delmulti(imo->imo_membership[i]); 2003 /* 2004 * Remove the gap in the membership array. 2005 */ 2006 for (++i; i < imo->imo_num_memberships; ++i) 2007 imo->imo_membership[i-1] = imo->imo_membership[i]; 2008 --imo->imo_num_memberships; 2009 crit_exit(); 2010 break; 2011 2012 default: 2013 error = EOPNOTSUPP; 2014 break; 2015 } 2016 2017 /* 2018 * If all options have default values, no need to keep the mbuf. 2019 */ 2020 if (imo->imo_multicast_ifp == NULL && 2021 imo->imo_multicast_vif == -1 && 2022 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 2023 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 2024 imo->imo_num_memberships == 0) { 2025 kfree(*imop, M_IPMOPTS); 2026 *imop = NULL; 2027 } 2028 2029 return (error); 2030 } 2031 2032 /* 2033 * Return the IP multicast options in response to user getsockopt(). 2034 */ 2035 static int 2036 ip_getmoptions(struct sockopt *sopt, struct ip_moptions *imo) 2037 { 2038 struct in_addr addr; 2039 struct in_ifaddr *ia; 2040 int error, optval; 2041 u_char coptval; 2042 2043 error = 0; 2044 switch (sopt->sopt_name) { 2045 case IP_MULTICAST_VIF: 2046 if (imo != NULL) 2047 optval = imo->imo_multicast_vif; 2048 else 2049 optval = -1; 2050 soopt_from_kbuf(sopt, &optval, sizeof optval); 2051 break; 2052 2053 case IP_MULTICAST_IF: 2054 if (imo == NULL || imo->imo_multicast_ifp == NULL) 2055 addr.s_addr = INADDR_ANY; 2056 else if (imo->imo_multicast_addr.s_addr) { 2057 /* return the value user has set */ 2058 addr = imo->imo_multicast_addr; 2059 } else { 2060 ia = IFP_TO_IA(imo->imo_multicast_ifp); 2061 addr.s_addr = (ia == NULL) ? INADDR_ANY 2062 : IA_SIN(ia)->sin_addr.s_addr; 2063 } 2064 soopt_from_kbuf(sopt, &addr, sizeof addr); 2065 break; 2066 2067 case IP_MULTICAST_TTL: 2068 if (imo == NULL) 2069 optval = coptval = IP_DEFAULT_MULTICAST_TTL; 2070 else 2071 optval = coptval = imo->imo_multicast_ttl; 2072 if (sopt->sopt_valsize == 1) 2073 soopt_from_kbuf(sopt, &coptval, 1); 2074 else 2075 soopt_from_kbuf(sopt, &optval, sizeof optval); 2076 break; 2077 2078 case IP_MULTICAST_LOOP: 2079 if (imo == NULL) 2080 optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 2081 else 2082 optval = coptval = imo->imo_multicast_loop; 2083 if (sopt->sopt_valsize == 1) 2084 soopt_from_kbuf(sopt, &coptval, 1); 2085 else 2086 soopt_from_kbuf(sopt, &optval, sizeof optval); 2087 break; 2088 2089 default: 2090 error = ENOPROTOOPT; 2091 break; 2092 } 2093 return (error); 2094 } 2095 2096 /* 2097 * Discard the IP multicast options. 2098 */ 2099 void 2100 ip_freemoptions(struct ip_moptions *imo) 2101 { 2102 int i; 2103 2104 if (imo != NULL) { 2105 for (i = 0; i < imo->imo_num_memberships; ++i) 2106 in_delmulti(imo->imo_membership[i]); 2107 kfree(imo, M_IPMOPTS); 2108 } 2109 } 2110 2111 /* 2112 * Routine called from ip_output() to loop back a copy of an IP multicast 2113 * packet to the input queue of a specified interface. Note that this 2114 * calls the output routine of the loopback "driver", but with an interface 2115 * pointer that might NOT be a loopback interface -- evil, but easier than 2116 * replicating that code here. 2117 */ 2118 static void 2119 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 2120 int hlen) 2121 { 2122 struct ip *ip; 2123 struct mbuf *copym; 2124 2125 copym = m_copypacket(m, MB_DONTWAIT); 2126 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 2127 copym = m_pullup(copym, hlen); 2128 if (copym != NULL) { 2129 /* 2130 * if the checksum hasn't been computed, mark it as valid 2131 */ 2132 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 2133 in_delayed_cksum(copym); 2134 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 2135 copym->m_pkthdr.csum_flags |= 2136 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 2137 copym->m_pkthdr.csum_data = 0xffff; 2138 } 2139 /* 2140 * We don't bother to fragment if the IP length is greater 2141 * than the interface's MTU. Can this possibly matter? 2142 */ 2143 ip = mtod(copym, struct ip *); 2144 ip->ip_len = htons(ip->ip_len); 2145 ip->ip_off = htons(ip->ip_off); 2146 ip->ip_sum = 0; 2147 if (ip->ip_vhl == IP_VHL_BORING) { 2148 ip->ip_sum = in_cksum_hdr(ip); 2149 } else { 2150 ip->ip_sum = in_cksum(copym, hlen); 2151 } 2152 /* 2153 * NB: 2154 * It's not clear whether there are any lingering 2155 * reentrancy problems in other areas which might 2156 * be exposed by using ip_input directly (in 2157 * particular, everything which modifies the packet 2158 * in-place). Yet another option is using the 2159 * protosw directly to deliver the looped back 2160 * packet. For the moment, we'll err on the side 2161 * of safety by using if_simloop(). 2162 */ 2163 #if 1 /* XXX */ 2164 if (dst->sin_family != AF_INET) { 2165 kprintf("ip_mloopback: bad address family %d\n", 2166 dst->sin_family); 2167 dst->sin_family = AF_INET; 2168 } 2169 #endif 2170 get_mplock(); /* is if_simloop() mpsafe yet? */ 2171 if_simloop(ifp, copym, dst->sin_family, 0); 2172 rel_mplock(); 2173 } 2174 } 2175