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