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