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