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