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