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