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