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