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