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