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